javaScript – Replace all occurrences of a forward slash in a string

Rajesh/ June 26, 2020/ javascript, web, webDev/ 0 comments

Today I learned an easy solution to replace all occurrences of a forward slash in string in javascript. I had to convert an URL to a string in another format, so initially, I tried str.replace() method like

str.replace('/', ":");

But to my surprise, it only replaced the first occurrence in the string.
Then I thought oh I might need to use a regular expression. But I failed to make a regular expression, I always do.
But then I find a much simple way.

str.split('/').join(':') 

Let me know if you find it useful.

Share this Post

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*