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?

Related Posts

  • 25 May

    Convert Seconds to Hours Minutes Seconds in Javascript

    Table of ContentsUsing javascript without any external libraryUsing momentJs libraryWas this post helpful? In this post, we will see how to convert seconds to Hours Minutes Seconds in JavaScript. There are multiple ways to do it. Let’s go through them. Using javascript without any external library To convert seconds to Hours Minutes Seconds in javascript: […]

  • 25 May

    Remove First Character from String in JavaScript

    Table of ContentsWays to Remove First Character from string in JavaScriptUsing substring()Using slice()Using substr()Using replace() methodUsing Array’s shift() with split() and join()ConclusionWas this post helpful? In this post, we will see how to remove first character from string in JavaScript. Since Strings are immutable in JavaScript, you can’t do it in-place. All the methods will […]

  • 25 May

    [Solved] TypeError: toLowerCase is not a function in JavaScript

    TypeError: .toLowerCase is not a function occurs when we call toLowerCase() function on object which is not an string. toLowerCase() function can be only called on string. To resolve this issue, convert value to string using toString() ,method before calling toLowerCase() method. Let’s see with help of simple example [crayon-62b8ea88e5a38878087079/] You will get following error […]

  • 25 May

    Escape Json String Containing Newline Characters in JavaScript

    Table of ContentsEscape JSON string in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape JSON string containing newline characters using JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape JSON string in JavaScript There is no direct way to escape JSON String […]

  • 23 May

    Escape Apostrophe in JavaScript

    Table of ContentsWays to escape Apostrophe in JavaScriptUsing escape characterUsing alternate String syntaxUsing template literalsWas this post helpful? In this article, we will see how to escape Apostrophe in JavaScript. Let’s see what happens if we have singe quote in String and we also use single quotes to declare the string in JavaScript. [crayon-62b8ea88e5c00274808215/] Output: […]

  • 23 May

    TypeError: foreach is not a function in JavaScript

    Table of ContentsUse Object.keys() or Object.values() or Object.entries()Use Arrays.from()Was this post helpful? TypeError: .foreach is not a function occurs when we call foreach() function on object which is not array, Set or Map. To resolve this issue, you can use Object.keys() rather than directly calling foreach() on object or use Array.from() to convert array like […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.