开发者

Javascript replace hypen with slash

开发者 https://www.devze.com 2022-12-15 05:39 出处:网络
How to replace a hypen (-) with a slash (\\) in Javascript? for example, I need to replace C-MyDocuments-VisualStudio2008-MyProjects

How to replace a hypen (-) with a slash (\) in Javascript?

for example, I need to replace

C-MyDocuments-VisualStudio2008-MyProjects

with

C\MyDocuments\VisualStudio2008\MyProjects

I tried replace function such as variable.r开发者_如何学编程eplace("-","\") but it showed me error of unterminated string constant.

I am working in VS 2008.


You need to escape the slash with an additional backslash like this:

variable = variable.replace("-","\\");

To replace the hyphen globally, try this:

variable = variable.replace(/-/g, "\\");

This uses a regular expression to search the string for the hyphen and the g modifier indicates that replacements should be global.


Try this:

variable.replace("-","\\")

You need to escape the slash char.


Try replacing with "\\" (two backslashes).

A single backslash escapes the following character.

0

精彩评论

暂无评论...
验证码 换一张
取 消