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

The following would do but only will replace one occurence:

"string".replace("https://stackoverflow.com/", 'ForwardSlash');

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

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

Leave a Comment