Removing backslashes from strings in javascript

I have a url in this format:

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

How to remove extra "\" from a string? I tried string.replace ("\", ""), but it does nothing. If you could give me a JavaScript regular expression that catches this, this will work too. I just need to catch this line when it is inside another line.

+5
source share
3 answers

Try:

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

This will specifically match the "\ /" pattern so that you do not accidentally delete any other backslashes that may be in the URL (for example, in the hash part).

+19
source

Try

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

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;
}
+1
source

All Articles