JavaScript

How to Replace All Single Quotes with Double Quotes in JavaScript

Text is typically stored and modified using JavaScript strings. Characters enclosed in quotation marks can be represented by strings. Strings can be written in one of three ways: inside single quotes, double quotes, or backticks. The use of single or double quotes generally makes no difference because they both represent strings at the end of the sentence.

Single quotes only have one drawback, they cannot be used within JSON files, which makes copying and pasting between JavaScript and JSON files difficult. JSON, however, only allows the use of double quotes.

This tutorial will demonstrate the methods for replacing single quotation marks with double quotes.

How to Replace All Single Quotes with Double Quotes in JavaScript?

For replacing single quotes with double quotes in a string, you can use the following JavaScript methods:

    • replace() method
    • replaceAll() method

Check out each of the mentioned methods one by one.

Method 1: Replace All Single Quotes with Double Quotes Using replace() Method

You can use the “replace()” method to replace the single quotations with double ones in a string. It is the predefined method of the String type object. It returns a new string as output with the replaced values after searching the string for a specific value or a regex pattern.

Syntax

Follow the given syntax to use the replace() method:

replace("searchValue", "replaceValue")

 
Here, the string value that will be replaced is “searchValue”, and the value that will be added in place of it is “replaceValue”.

Example 1: Replace First Occurrence of Single Quotes with Double Quotes

First, we will create a variable that stores a string name “strng”:

var strng = "Welcome to 'LinuxHint'."

 
Now, we will call the replace() method by passing a single quote and a double quote as arguments. We will use backticks to represent the search value and the replaced value rather than single or double quotes due to differentiation:

console.log(strng.replace(`'`, `"`));

 
Output shows that the replace() method only replaced the first occurrence of a single quote to a double quote from the string:


Need to replace all the occurrences of the single quotes using replace() method from a string? Follow the given section.

Example 2: Replace All Occurrences of Single Quotes with Double Quotes

Here, we will consider the same string named “strng” and replace all the single quotes from the string with double quotes, using regex “/’ /g”. For matching all the occurrences of a single quote in the string, we will use the “g” (global) flag in our regex:

console.log(strng.replace(/'/g, `"`));

 
As you can see, the output indicates that all the occurrences of a single quote are successfully replaced with double quotes:


Let’s see another method for replacing all the occurrences of the single quote with double quotes.

Method 2: Replace All Single Quotes with Double Quotes Using replaceAll() Method

In the “replaceAll()” method, you do not need to add a regex for replacing all the occurrences from a string. It accepts two parameters; one is the value going to be searched, and the other is the replaced value.

Syntax

Use the following syntax for the replaceAll() method:

replaceAll("searchValue", "replaceValue")

 
Example

In this example, we will create a string with multiple words that are surrounded by single quotes:

var strng = "Welcome to 'LinuxHint', it is the best 'Website' for Learning."

 
Now, we will call the replaceAll() method by passing the single quote () as the searched value and the double quote () as a replaced value:

console.log(strng.replaceAll(`'`, `"`));

 
The output indicates that the replaceAll() method successfully replaced all the occurrences of the single quotes with the double quotes from the string:


We gathered all the methods for replacing single quotes with double quotes in a string.

Conclusion

For replacing single quotes with double quotes in a string, you can use the JavaScript method, including replace() method and the replaceAll() method. Only the first occurrence is replaced by the replace() method; to replace all instances, use the regex in it. In contrast, the replaceAll() method replaced all the instances of the single quotations with double quotes. In this tutorial, we have demonstrated the methods for replacing single quotes with double quotes with detailed explanations.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.