Javascript 从javascript中的字符串中删除反斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4870769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:32:20  来源:igfitidea点击:

Removing backslashes from strings in javascript

javascripthtmlregexstring

提问by Franz Payer

I have a url in this format:

我有一个这种格式的网址:

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

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.

如何从字符串中删除多余的“\”?我试过 string.replace("\","") 但这似乎没有任何作用。如果你能给我一个 JavaScript 正则表达式来捕捉这个,那也可以。当它在另一个字符串中时,我只需要能够捕获这个字符串。

回答by Pointy

Try

尝试

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

回答by Ates Goral

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 中可能存在的任何其他反斜杠(例如,在哈希部分)。

回答by bhowden

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;
}