Reputation: 65
I have a javascript file that is ran through a windows job using cscript. However, I can't seem to fix this thing to work correctly. Inside the file, it basically takes a URL and transforms it to a UNC path.
ex: http://mysite.com/document1.htm to \myserver\document1.htm
However, I can't seem to get the /'s to goto \'s and am at a loss why.
I've tried 2 things basically
1) str = str.replace(/\/g, "\\");
2) str = str.replace("/", "\\");
Any idea why it wont work?
Thanks, Dave
Upvotes: 2
Views: 8696
Reputation: 173
You can use the following trick:
str = str.split("/").join("\\");
More generally:
function replaceAll(str, a, b) {
return str.split(a).join(b);
}
This avoids regular expression nightmares.
Upvotes: 3
Reputation: 630399
It's like this:
str = str.replace(/\//g, "\\");
the /
on the end is the normal /pattern/
format, you need an extra for your \
escape, you can test it out here.
Upvotes: 9