简体   繁体   English

Javascript:如何将所有“ \\”字符替换为“”

[英]Javascript: How to replace all “\” character to “”

I try to replace "\\" character to empty "" but I can't do it. 我尝试将“ \\”字符替换为空的“”,但我做不到。 How to solve it? 怎么解决呢?

Here is my code 这是我的代码

abc = abc.replace('\','');

要替换字符串中的所有匹配项,您需要使用带有g标志的正则表达式,

abc = abc.replace(/\\/g, '');

I think you need to escape the backslash: 我认为您需要避开反斜杠:

abc = abc.replace('\\', '');

If you want to replace all occurrences, you can try: 如果要替换所有出现的内容,可以尝试:

var pattern = '\\\\';
var re = new RegExp(pattern, 'g');
abc = abc.replace(re, '');

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

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