最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to replace all " in a JS string? - Stack Overflow

programmeradmin2浏览0评论

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

Share Improve this question asked Apr 23, 2012 at 14:58 Danny FoxDanny Fox 40.8k29 gold badges71 silver badges96 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 5

You 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);
​
发布评论

评论列表(0)

  1. 暂无评论