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.

Answers:

Answer

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).

Answer

Try

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

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.