Reputation: 832
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
Reputation: 49
let str = '"Value"';
str = str.replace(/"|'/g, '');
output = Value
Upvotes: 2
Reputation: 803
I hope this will work:
var value = '"fetchedValue"';
value = value.replace(/^"|"$/g, '');
Upvotes: 9
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
Reputation: 1403
It's weird. Your solution should work.
Well, try this:
var value = "fetchedValue";
value = value.slice(1, -1);
Upvotes: 10