Sometimes, we want to replace forward slash "/ " character in a JavaScript string.
In this article, we’ll look at how to replace forward slash "/ " character in a JavaScript string.
How to replace forward slash "/ " character in a JavaScript string?
To replace forward slash "/ " character in a JavaScript string, we can use the string replace
method.
For instance, we write
const str = someString.replace(/\//g, "-");
to call someString.replace
with the /\//g
regex and '-'
to replace all forward slashes with dashes.
The g
flag makes replace
replace all matches.
Conclusion
To replace forward slash "/ " character in a JavaScript string, we can use the string replace
method.