2012-09-24 113 views
1

我愿做这样的事情:如何用双引号替换“?

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 

但对我来说,这并不工作。我尝试了所有的方法。

回答

2

您可以使用Unicode值替换:

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

DEMO:http://jsfiddle.net/uM2fY/

1

所以我们打开控制台,并请参阅:

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

,并用括号:

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

一切工作就像你想的一样。请检查您的输入字符串。