Categories
JavaScript Answers

How to replace single quotes with double quotes in JavaScript?

Spread the love

Sometimes, we want to replace single quotes with double quotes in JavaScript.

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

How to replace single quotes with double quotes in JavaScript?

To replace single quotes with double quotes in JavaScript, we use the string replace method.

For instance, we write

const a = "[{'column1':'value0','column2':'value1','column3':'value2'}]";
const b = a.replace(/\'/g, '"');

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

We use \' to match single quotes.

The g flag makes replace replace all matches.

The string with the replacements done is returned.

Conclusion

To replace single quotes with double quotes in JavaScript, we 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 *