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

查看:186
本文介绍了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天全站免登陆