Javascript replace hypen with slash
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.
精彩评论