Skip to content Skip to sidebar Skip to footer

Java script remove backslashes and forwardslashes from a string

Reading Time: < 1 minute

How can I remove backslashes and forwardslashes from a string or remove forwardslash from a url using java script.I’m going to explain to do this by using regular expressions and simple core java script.

1-Remove Backslashes

Backslash (\)

var myString = 'Hi how are\ you?';
myString = myString.replace("\\", "");
//myString = myString.replaceAll("\\", ""); // also this is can be use
alert(myString); // Hi how are you?

2-Remove Forwardslashes

Forwardslash (/)

var myString = '';
myString = myString.replace(/\//g,'');
//myString = myString.replace(/\/$/,''); // same effect, Note here " $ " mark
alert(myString); //

Note here is the regular expression /\//g and /\/$/. The piece of the string you want replacing is written between the first and last forward slashes.
If you wanted the word ‘book’ replaced you would write /book/g.