The following would do but only will replace one occurence:
For a global replacement, or if you prefer regular expressions, you just have to escape the slash:
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);
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!'
Is this what you want?
'string with / in it'.replace(/\//g, '\\');
This has worked for me in turning
"//" into just
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');