在javascript中删除字符串中的反斜杠

时间:2022-05-28 22:28:55

I have a url in this format:

我有这种格式的网址:

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

HTTP:\ / \ / example.example.ru \ / u82651140 \ /音频\ /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.

如何从字符串中删除额外的“\”?我尝试过string.replace(“\”,“”),但似乎没有做任何事情。如果你能给我一个可以捕获它的JavaScript正则表达式,那也可以。我只需要能够在另一个字符串中捕获该字符串。

3 个解决方案

#1


20  

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 (e.g. in the hash part).

这将特别匹配“\ /”模式,以便您不会无意中删除URL中可能存在的任何其他反斜杠(例如,在散列部分中)。

#2


15  

Try

尝试

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

#3


2  

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

来自: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;
}

#1


20  

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 (e.g. in the hash part).

这将特别匹配“\ /”模式,以便您不会无意中删除URL中可能存在的任何其他反斜杠(例如,在散列部分中)。

#2


15  

Try

尝试

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

#3


2  

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

来自: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;
}