简体   繁体   中英

Removing backslashes from strings in javascript

I have a url in this format:

http:\/\/example.example.ru\/u82651140\/audio\/song.mp3

How can I remove the extra "\"s from the string? I have tried string.replace("\","") but that does not seem to do anything. If you could give me a JavaScript regular expression that will catch this, that would also work too. I just need to be able capture this string when it is inside another string.

试试

str = str.replace(/\\/g, '');

Try:

string.replace(/\\\//g, "/");

This will specifically match the "\\/" pattern so that you don't unintentionally remove any other backslashes that there may be in the URL (eg in the hash part).

from: http://knowledge-serve.blogspot.com/2012/08/javascript-remove-all-backslash-from.html

function replaceAllBackSlash(targetStr){
    var index=targetStr.indexOf("\\");
    while(index >= 0){
        targetStr=targetStr.replace("\\","");
        index=targetStr.indexOf("\\");
    }
    return targetStr;
}
Join Two Portions of a Path using
    function join(port1, port2) {
        let p1 = port1.split('/').join('');
        let p2 = port2.split('/').join('');
        let np = p1 + '/' +  p2;
        return np;
    }
    console.log(join("port1/", "/port2"))

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