Remove Double Quotes from a String in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Remove Double Quotes from a String #

Use the String.replaceAll() method to remove all double quotes from a string, e.g. str.replaceAll('"', ''). The replace() method will return a new string with all double quotes removed.

index.js
const str = 'hel"l"o wor"l"d'; const withoutQuotes = str.replaceAll('"', ''); console.log(withoutQuotes); // 👉️ hello world

We passed the following parameters to the String.replaceAll method:

  1. a substring we want to match in the string. In our case a string containing a double quote.
  2. The replacement for each match. We need to remove each double quote, so we replace double quotes with empty strings.
The replaceAll() method does not change the original string. It returns a new string with all matches replaced. Strings are immutable in JavaScript.

Alternatively, you can use the replace() method.

To remove double quotes from a string:

  1. Call the replace() method on the string.
  2. The replace method will replace each occurrence of a double quote with an empty string.
  3. The replace method will return a new string with all double quotes removed.
index.js
const str = 'hel"l"o wor"l"d'; const withoutQuotes = str.replace(/"/g, ''); console.log(withoutQuotes); // 👉️ hello world

The first parameter we passed to the String.replace method is a regular expression that we want to match in the string.

The two forward slashes / / mark the beginning and end of the regular expression.

We use the g (global) flag because we want to match all occurrences of a double quote in the string, and not just the first occurrence.

If you ever need help reading a regular expression, check out this regex cheatsheet by MDN.

The second parameter we passed to the replace method is an empty string because we want to remove all double quotes from the string.

The replace method doesn't mutate the original string, it returns a new string.

Alternatively, you can use the split() and join() methods.

To remove double quotes from a string:

  1. Use the String.split() method to split the string on each double quote.
  2. Use the join() method to join the array of strings.
  3. The new string won't contain any double quotes.
index.js
const str = 'hel"l"o wor"l"d'; const withoutQuotes = str.split('"').join(''); console.log(withoutQuotes); // 👉️ hello world

The String.split method splits the string into an array of substrings based on the provided separator.

index.js
const str = 'hel"l"o wor"l"d'; const split = str.split('"'); // 👉️ ['hel', 'l', 'o wer', 'l', 'd'] console.log(split);

The last step is to use the Array.join method to concatenate the array elements into a string.

My preferred approach is to use the replaceAll() method as it is the most readable of the 3 options.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.