2

I would like to do something like this:

 s = s.replace(/(”)/g, '"'); // need to replace with double quotes
 s = s.replace(/(“)/g, '"'); // need to replace with double quotes
 s = s.replace(/(’)/g, "'"); // need to replace with single quotes

But for me this does not work. I tried all the ways.

4

2 回答 2

3

您可以在替换中使用 Unicode 值:

s = s.replace(/\u201C|\u201D/g, '"');  // for “ or ”
s = s.replace(/\u2019/g, "'");         // for ’

演示:http: //jsfiddle.net/uM2fY/

于 2012-09-24T23:29:00.010 回答
3

所以我们打开控制台,看看:

>>> 'test&rqduo;foo'.replace(/&rqduo;/g, '\"' );
test"foo

并带大括号:

>>> 'test&rqduo;foo'.replace(/(&rqduo;)/g, '\"' );
test"foo

一切都如你所想。请检查您的输入字符串。

于 2012-09-24T23:29:39.887 回答