Javascript 和反斜杠替换 [英] Javascript and backslashes replace

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

问题描述

这是我的字符串:

var str = "This is my string";

这是我的代码:

var replaced = str.replace("/\/", "\\");

我无法得到我的输出:

"This is my \string"

对于正则表达式和替换值,我尝试了所有我能想到的组合.

I have tried every combination I can think of for the regular expression and the replacement value.

感谢任何帮助!

推荐答案

多年来一直被这个问题难住,所有的答案都坚持认为源字符串需要已经在其中转义反斜杠......这并不总是案例.

Got stumped by this for ages and all the answers kept insisting that the source string needs to already have escaped backslashes in it ... which isn't always the case.

这样做..

var replaced = str.replace(String.fromCharCode(92),String.fromCharCode(92,92));

这篇关于Javascript 和反斜杠替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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