Categories
JavaScript Answers

How to replace both double and single quotes in JavaScript string?

Spread the love

Sometimes, we want to replace both double and single quotes in JavaScript string.

In this article, we’ll look at how to replace both double and single quotes in JavaScript string.

How to replace both double and single quotes in JavaScript string?

To replace both double and single quotes in JavaScript string, we call the replace method.

For instance, we write

const newStr = str.replace(/["']/g, "");

to call str.replace with a regex that matches all single and double quotes.

And we replace them all with empty strings.

The g flag makes replace replace all matches.

Conclusion

To replace both double and single quotes in JavaScript string, we call the 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 *