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

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.