re: javaScript – Replace all occurrences of a forward slash in a string VIEW POST


If you still need the regex way of doing this:

str.replace(/\//g, ":")

Explanation: anything enclosed within a pair of forward slashes / signals that it is a regex. As such, we need to use the backslash to escape the forward slash to get \/. The g at the end is a flag to perform a global search, and thus replaces all occurrence of / with :.


One and done function is better than two! Use this all the time.

Code of Conduct Report abuse