Javascript用斜杠替换hypen

xor*_*wer 0 .net javascript asp.net javascript-events visual-studio-2008

如何-\javascript中的斜杠()替换hypen()?

例如,我需要更换

C-我的文档,VisualStudio2008-的MyProjects

c ^ \我的文档\ VisualStudio2008\MyProjects下

我尝试替换功能,variable.replace("-","\")但它显示我未终止字符串常量的错误

我在VS 2008工作

谢谢

And*_*are 9

你需要使用这样的额外反斜杠来逃避斜杠:

variable = variable.replace("-","\\");
Run Code Online (Sandbox Code Playgroud)

要全局替换连字符,请尝试以下操作:

variable = variable.replace(/-/g, "\\");
Run Code Online (Sandbox Code Playgroud)

这使用正则表达式在字符串中搜索连字符,g修饰符指示替换应该是全局的.