Vishesh
Vishesh

Reputation: 832

Remove quotes in javascript

I am getting data in a variable as ""Value"". So, I want to remove quotes but only inner quotes. I tried with this but it's not working.

var value = "fetchedValue";
value = value.replace(/\"/g, "");

Can anyone explain with an example?

expected = "Value"

Upvotes: 7

Views: 21980

Answers (4)

preetham kp
preetham kp

Reputation: 49

let str = '"Value"';
str = str.replace(/"|'/g, '');

output = Value

Upvotes: 2

Kishan Patel
Kishan Patel

Reputation: 803

I hope this will work:

var value = '"fetchedValue"';
value = value.replace(/^"|"$/g, '');

Upvotes: 9

Hardik Shah
Hardik Shah

Reputation: 4210

This will take care about existing of two quotes ""

function removeQuotes(a) {
    var fIndex = a.indexOf('""');
    var lIndex = a.lastIndexOf('""');
    if(fIndex >= 0 && lIndex >= 0){
      a = a.substring(fIndex+1, lIndex+1);
    }
    return a;
    
}

console.log(removeQuotes('"Foo Bar"'));
console.log(removeQuotes('""Foo Bar""'));

Upvotes: 0

Sagar Chaudhary
Sagar Chaudhary

Reputation: 1403

It's weird. Your solution should work.
Well, try this:

var value = "fetchedValue";
value = value.slice(1, -1);

Upvotes: 10

Related Questions