How to replace all \"
to "
in a string?
I tried, but it doesn't works: var foobar = ("foo\\\"bar\\\"foo").replace(/"\\\""/,'"');
The result is foo\"bar\"foo
, but it should be foo"bar"foo
How to replace all \"
to "
in a string?
I tried, but it doesn't works: var foobar = ("foo\\\"bar\\\"foo").replace(/"\\\""/,'"');
The result is foo\"bar\"foo
, but it should be foo"bar"foo
6 Answers
Reset to default 5You don't need to use quotes inside of a RegEx pattern, the //
delimiters act as ones.
var foobar = "foo\\\"bar\\\"foo".replace(/\\"/g,'"');
Works for me.
Try .replace(/\\"/g,'"');
- regexes don't need quotes around them, I'm surprised you get any result at all.
You need to fix your regex, you need to do
replace(/\\\"/g, "\"")
Your quoting is wrong and you're not using g - global flag. It should be:
var foobar = ("foo\\\"bar\\\"foo").replace(/\\"/g,'"');
Try defining it like this
var foobar = ("foo\\\"bar\\\"foo").replace(/"\\\""/g,'"');
note that the .replace
has a /g
which makes it global
jsfiddle
// initial string
var str = "AAAbbbAAAccc";
// replace here
str = str.replace(/A/g, "Z");
alert(str);