javascript 如何更换“ &ldquo 带双引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12574075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:33:01  来源:igfitidea点击:

How to replace “ &ldquo with double quotes?

javascriptregex

提问by nimi

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.

但对我来说这是行不通的。我尝试了所有方法。

回答by VisioN

You can use Unicode values in replace:

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

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

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

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

回答by c69

So we open console, and see:

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

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

and with braces:

并带括号:

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

Everything work just like you thought. Please, check your input strings.

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