简体   繁体   English

JavaScript不会替换字符串的最后一个符号

[英]JavaScript does not replace the last symbol of the string

I have try to replace a symbol in JavaScript, but somehow this is always only replace the 1st symbol of the string instead of replaced all the symbol. 我试图替换JavaScript中的符号,但是总会以某种方式仅替换字符串的第一个符号,而不是替换所有符号。

JavaScript : JavaScript

var note = "test'test'test'";
note = note .replace("'", "'");

Output : 输出

test'test'test'

Does anyone know how can I can replace all ' symbols with ' 有谁知道我怎么能用'替换所有的'符号 ?? ??

Use regex substitution and add a g flag to make it global: 使用正则表达式替换并添加g标志使其全局:

> "test'test'test'".replace(/'/g, ''');
"test'test'test'"

Use g suffix for the Global substituation. g后缀用于全局替换。

This is the right way: 这是正确的方法:

note = "test'test'test'";
note.replace(/\'/g,"'")

Check this: jsfiddle 检查一下: jsfiddle

试试这个笔记.replace(/ \\'/ g,''');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM