简体   繁体   中英

How to replace a backslash character with a semicolon?

I'm trying to do a simple replace of a backslash occurence in a string. So I used the string.replace method, passing in a regex for the backslash to be removed \\ .

But I noticed when I invoke this method, instead of replacing the backslash after EMEA with a colon : character. It just removes the first letter in the username.

I've made a JSFIddle of the code here .

Not sure why the regex doesn't work as it's suggested in other SO answers here:

Replace all backslashes in a string with a pipe

Question:

How can you replace a backslash character with a semicolon?

Code gist:

var str = "EMEA\victorb";
str = str.replace(/\\/g, ':');

document.write(str);

That's a problem with the original string, not with the regex: it's

var str = "EMEA\victorb";

When it shold be:

var str = "EMEA\\victorb";
var str = "EMEA\\victorb";
    str = str.replace("\\", ':'); because if you let just one \ it will be ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM