替换方法不起作用 [英] Replace method doesn't work

查看:116
本文介绍了替换方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换智能报价,例如''到常规报价。另外,我想替换©®。我使用了以下代码。但它没有帮助。
请帮我解决此问题。

I want to replace the smart quotes like , , " and " to regular quotes. Also, I wanted to replace the ©, ® and . I used the following code. But it doesn't help. Kindly help me to resolve this issue.

str.replace(/[""]/g, '"');
str.replace(/[‘’]/g, "'");


推荐答案

使用:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g, "'");

或在一个声明中执行:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

在JavaScript中(和许多其他语言一样)字符串是不可变的 - 字符串替换方法实际上只返回新字符串而不是修改字符串。

In JavaScript (as in many other languages) strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place.

的MDN JavaScript参考条目替换状态:

The MDN JavaScript reference entry for replace states:


返回一个新的字符串,其中一个或所有匹配的模式被替换替换。

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

此方法不会更改调用它的String对象。它只返回一个新字符串。

This method does not change the String object it is called on. It simply returns a new string.

这篇关于替换方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆