user1292656
user1292656

Reputation: 2560

Javascript substring with escape characters

i have a string like the following

var string1 = " folder\\subfolder1\\subfolder2 "

and i want to replace the "\" with "/" with string1.replace in order to be

var string2 = "folder/subfolder1/subfolder2"

I tried

            var rep = path.replace("\\", "/");

but is not working. Any help ?

Upvotes: 0

Views: 611

Answers (1)

Alex K.
Alex K.

Reputation: 175776

You need to do it globally, for example with a regexp literal & the g flag;

var rep = path.replace(/\\/g, "/");

Upvotes: 3

Related Questions