Remove Double Quotes from String in JavaScript

In this post, we will see how to remove double quotes from String in JavaScript.

Ways to Remove Double Quotes from String in JavaScript

There are multiple ways to do it. Let’s go through them.

Using replace()

To remove double quotes from String in JavaScript:

  • Use replace() method with regular expression /"/g as first parameter and empty string as second parameter.

Output:

The replace() method returns a new string with matches of pattern replaced by a replacement string.
It takes 2 parameters.

  1. Regular expression which we want to match in String.
  2. Replacement String – String with which we want to replace matched string.

Let’s go through regular expression /"/g used in above replace method.

  • The two forward slashes mark start and end of regular expression
  • " to match double quotes in the string.
  • g denotes global replacement here. It is flag at end of regex which depicts that we want to remove all the double quotes not only first one.

replace() returns a new String, it does not change contents of original String.

Using replaceAll()

To remove comma from string in JavaScript:

  • Use replaceAll() method with double quotes(") as first parameter and empty string as second parameter. replaceAll() method returns new String with all matches replaced by given replacement.

The replaceAll() method returns a new string with matches of passed string replaced by a replacement string.
It takes 2 parameters.

  1. substring which we want to match in String.
  2. Replacement String – String with which we want to replace matched string.

Output:

replaceAll() method is not supported in Internet Explorer versions 6-11. Use replace method in case you are using them.

Using split() and join() method

To remove double quotes from String in JavaScript:

This method is useful, when you want to avoid regular expressions in replace() method.

Output:

We used split() method to split the String on each occurrence of double quotes(") into array.

Then joined the array elements with empty String using join() method.

Using for loop

To remove double quotes from String in JavaScript:

  • Iterate over string using for loop
  • In each iteration, if character is not ", we include it in strWithoutQuotes
  • If character is double quote ", then we do nothing and continue the loop.

Output:

Conclusion

We discussed different methods to remove double quotes in String in JavaScript. First two method i.e. remove() and removeAll() work very similar.

Third approach makes use of split() and join() concept which is often used to remove character from String. Idea is to split string by double quotes and join by empty string.

Final approach uses for loop to remove double quotes from String. I would avoid this approach as it involves boiler plate code.

That’s all about how to remove double quotes from string in JavaScript.

Was this post helpful?

Leave a Reply

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