How to globally replace a forward slash in a JavaScript string?

Javascript

Javascript Problem Overview


How to globally replace a forward slash in a JavaScript string?

Javascript Solutions


Solution 1 - Javascript

The following would do but only will replace one occurence:

"string".replace('/', 'ForwardSlash');

For a global replacement, or if you prefer regular expressions, you just have to escape the slash:

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

Solution 2 - Javascript

Use a regex literal with the g modifier, and escape the forward slash with a backslash so it doesn't clash with the delimiters.

var str = 'some // slashes', replacement = '';
var replaced = str.replace(/\//g, replacement);

Solution 3 - Javascript

You need to wrap the forward slash to avoid cross browser issues or //commenting out.

str = 'this/that and/if';

var newstr = str.replace(/[/]/g, 'ForwardSlash');

 

Solution 4 - Javascript

Without using regex (though I would only do this if the search string is user input):

var str = 'Hello/ world/ this has two slashes!';
alert(str.split('/').join(',')); // alerts 'Hello, world, this has two slashes!' 

Solution 5 - Javascript

This has worked for me in turning "//" into just "/".

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

Solution 6 - Javascript

Is this what you want?

'string with / in it'.replace(/\//g, '\\');

Solution 7 - Javascript

Hi a small correction in the above script.. above script skipping the first character when displaying the output.

function stripSlashes(x)
{
var y = "";
for(i = 0; i < x.length; i++)
{
    if(x.charAt(i) == "/")
    {
        y += "";
    }
    else
    {
        y+= x.charAt(i);
    }
}
return y;   
}

Solution 8 - Javascript

This is Christopher Lincolns idea but with correct code:

function replace(str,find,replace){
    if (find != ""){
        str = str.toString();
        var aStr = str.split(find);
        for(var i = 0; i < aStr.length; i++) {
            if (i > 0){
                str = str + replace + aStr[i];
            }else{
                str = aStr[i];
            }
        }
    }
    return str;
}

Example Usage:

var somevariable = replace('//\\\/\/sdfas/\/\/\\\////','\/sdf','replacethis\');

Javascript global string replacement is unecessarily complicated. This function solves that problem. There is probably a small performance impact, but I'm sure its negligable.

Heres an alternative function, looks much cleaner, but is on average about 25 to 20 percent slower than the above function:

function replace(str,find,replace){
    if (find !== ""){
        str = str.toString().split(find).join(replace);
    }
    return str;
}

Solution 9 - Javascript

var str = '/questions'; // input: "/questions"
while(str.indexOf('/') != -1){
   str = str.replace('/', 'http://stackoverflow.com/');
}
alert(str); // output: "http://stackoverflow.com/questions"

The proposed regex /\//g did not work for me; the rest of the line (//g, replacement);) was commented out.

Solution 10 - Javascript

You can create a RegExp object to make it a bit more readable

str.replace(new RegExp('/'), 'foobar');

If you want to replace all of them add the "g" flag

str.replace(new RegExp('/', 'g'), 'foobar');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBabikerView Question on Stackoverflow
Solution 1 - JavascriptSeldaekView Answer on Stackoverflow
Solution 2 - JavascriptBoltClockView Answer on Stackoverflow
Solution 3 - JavascriptBazView Answer on Stackoverflow
Solution 4 - JavascriptPleaseStandView Answer on Stackoverflow
Solution 5 - JavascriptMartyView Answer on Stackoverflow
Solution 6 - JavascriptHemlockView Answer on Stackoverflow
Solution 7 - JavascriptVinayView Answer on Stackoverflow
Solution 8 - JavascriptJeff LuyetView Answer on Stackoverflow
Solution 9 - JavascriptChristopher LincolnView Answer on Stackoverflow
Solution 10 - JavascriptrioV8View Answer on Stackoverflow