7

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"
N.chauhan
  • 263
  • 1
  • 8
Vishesh
  • 817
  • 2
  • 7
  • 16
  • 2
    Your current `value` has no quotes in the variable - it's a string. Did you mean `var value = '"fetchedValue"';`? – CertainPerformance Mar 24 '18 at 09:32
  • 5
    Possible duplicate of [I want to remove double quotes from a String](https://stackoverflow.com/questions/19156148/i-want-to-remove-double-quotes-from-a-string) – Momin Mar 24 '18 at 09:34
  • Your example `var value = "fetchedValue"; value = value.replace(/\"/g, "");` makes no sense. There are no double quote characters in the 'value' variable initially. Also, what *specific* issue are you getting. Saying it's "not working" isn't helpful, describe how it isn't working and any console (or other) errors you're getting – Jayce444 Mar 24 '18 at 09:36
  • @CertainPerformance No I was getting data as ""FetchedValue"" but now it's working fine for me with value = value.replace(/\"/g, ""); – Vishesh Mar 24 '18 at 09:45
  • Please provide an example that actually demonstrates the behaviour you're experiencing. Your `value` does **not** have any quotes in it, so people cannot help you correctly. – James Monger Oct 10 '18 at 10:41

4 Answers4

9

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

var value = "fetchedValue";
value = value.slice(1, -1);
Sagar Chaudhary
  • 1,343
  • 7
  • 21
9

I hope this will work:

var value = '"fetchedValue"';
value = value.replace(/^"|"$/g, '');
Kishan Patel
  • 778
  • 11
  • 26
2
let str = '"Value"';
str = str.replace(/"|'/g, '');

output = Value

4b0
  • 21,981
  • 30
  • 95
  • 142
  • 1
    Welcome to Stack Overflow! It's good practice on StackOverflow to add an explanation as to why your solution should work. – 4b0 Jan 04 '21 at 08:04
0

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""'));
Hardik Shah
  • 4,042
  • 2
  • 20
  • 41