views:

73

answers:

1

Hello,

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

+4  A: 

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.

Nick Craver
And `str = str.replace("/", "\\");` does not work because, unless otherwise noted (by a regex with the `g` modifier), JavaScript only replaces the first occurrence.
Tomalak
is that just the regex for \ or is there another escape char in there that I can't see. It works fine but would like to understand why
Dave
@Nick, that is the world's greatest link! I've been looking for something like this forever.
EmmyS
@Dave - The ` \ ` is just to escape the ` / `, is that what you mean? I'm unclear on your comment.
Nick Craver
Pretty much. I was thinking the // was / escaped.
Dave