我想用安全\
替换替换反斜杠=>'\' .
但是当我申请替换'\'时,我的代码替换所有'#'失败:
el = el.replace(/\#/g, '#'); // replaces all '#' //that's cool
el = el.replace(/\\/g, '\'); // replaces all '\' //that's failing
Run Code Online (Sandbox Code Playgroud)
为什么?
Fun*_*bat 11
打开控制台和类型
'\'.replace(/\\/g, '\');
Run Code Online (Sandbox Code Playgroud)
失败,因为字符串中的斜杠不是真正在字符串中,它正在逃避'
'\\'.replace(/\\/g, '\');
Run Code Online (Sandbox Code Playgroud)
有效,因为它需要一个斜杠并找到它.
你的正则表达式工作.