Categories
JavaScript Answers

How to replace forward slash “/ ” character in a JavaScript string?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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