Reputation:
I have a string like (which is a shared path)
\\cnyc12p20005c\mkt$\\XYZ\
I need to replace all \\
with single slash so that I can display it in textbox. Since it's a shared path the starting \\
should not be removed. All others can be removed.
How can I achieve this in JavaScript?
Upvotes: 1
Views: 1050
Reputation: 344605
You could do it like this:
var newStr = str.replace(/(.)\\{2}/, "$1\\");
Or this, if you don't like having boobs in your code:
var newStr = "\\" + str.split(/\\{1,2}/).join("\\");
Upvotes: 4
Reputation: 22943
You can use regular expression to achieve this:
var s = '\\\\cnyc12p20005c\\mkt$\\\\XYZ\\';
console.log(s.replace(/.\\\\/g, '\\')); //will output \\cnyc12p20005c\mkt$\XYZ\
Double backslashes are used because backslash is special character and need to be escaped.
Upvotes: 0