How to remove Quotes from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
6 min

banner

# Table of Contents

  1. Remove all Quotes from a String in JavaScript
  2. Removing only the enclosing quotes from a String
  3. Remove all quotes from a String using String.replace()
  4. Remove the double quotes from a string using String.split()
  5. Remove the double quotes from a string using a for loop

# Remove all Quotes from a String in JavaScript

Use the String.replaceAll() method to remove all quotes from a string, e.g. str.replaceAll('"', '').

The replaceAll() method will remove all quotes from the string by replacing them with empty strings.

index.js
const str = 'hel"l"o wor"l"d'; // โœ… Remove double quotes from a string const withoutQuotes = str.replaceAll('"', ''); console.log(withoutQuotes); // ๐Ÿ‘‰๏ธ hello world // --------------------------------------------- // โœ… Remove double and single quotes from a string const str2 = 'a"b \'c "d \'e'; const withoutQuotes2 = str2 .replaceAll('"', '') .replaceAll("'", ''); console.log(withoutQuotes2); // ๐Ÿ‘‰๏ธ ab c d e

remove all quotes from string

The code for this article is available on GitHub

If you only need to remove the single quotes from a string, use the following code sample.

index.js
// โœ… Remove single quotes from a string const str = "a 'b' c 'd' e"; const result = str.replaceAll("'", ''); console.log(result); // ๐Ÿ‘‰๏ธ a b c d e

Notice that we alternate between double and single quotes. This is important because otherwise we'd terminate the string prematurely and get a syntax error.

The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
We removed all double quotes from the string by replacing each occurrence of a double quote with an empty string.

The String.replaceAll() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

# Removing only the enclosing quotes from a String

If you need to remove only the enclosing quotes from a string, use the String.slice() method.

index.js
// โœ… remove only the enclosing double quotes let str = '"hello world"'; if (str.at(0) === '"' && str.at(-1) === '"') { str = str.slice(1, -1); } console.log(str); // ๐Ÿ‘‰๏ธ hello world

remove only enclosing quotes from string

The code for this article is available on GitHub

We used the String.at() method to check if the string starts with and ends with double quotes.

If the condition is met, we use the String.slice() method to remove the first and the last characters from the string.

Notice that we used the let keyword when declaring the str variable. This is important because variables declared using const cannot be reassigned.

The same approach can be used to remove only the enclosing single quotes from a string.

index.js
// โœ… Remove only the enclosing single quotes let str = "'hello world'"; if (str.at(0) === "'" && str.at(-1) === "'") { str = str.slice(1, -1); } console.log(str); // ๐Ÿ‘‰๏ธ hello world

Alternatively, you can use the String.replace() method.

# Remove all quotes from a String using String.replace()

This is a three-step process:

  1. Call the replace() method on the string.
  2. The replace method will replace each occurrence of a quote with an empty string.
  3. The replace method will return a new string with all quotes removed.
index.js
// โœ… Remove double quotes from a string const str = 'hel"l"o wor"l"d'; const withoutQuotes = str.replace(/"/g, ''); console.log(withoutQuotes); // ๐Ÿ‘‰๏ธ hello world // --------------------------------------------- // โœ… Remove double and single quotes from a string const str2 = 'a"b \'c "d \'e'; const withoutQuotes2 = str2.replace(/['"]+/g, ''); console.log(withoutQuotes2); // ๐Ÿ‘‰๏ธ ab c d e

remove all quotes from string using string replace

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

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

index.js
const str = 'hel"l"o wor"l"d'; const withoutQuotes = str.replace(/"/g, ''); console.log(withoutQuotes); // ๐Ÿ‘‰๏ธ hello world
We used the g (global) flag because we want to match all occurrences of a double quote in the string and not just the first occurrence.

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

If you need to remove all double and single quotes from the string, pass a character class to the String.replace() method.

index.js
const str2 = 'a"b \'c "d \'e'; const withoutQuotes2 = str2.replace(/['"]+/g, ''); console.log(withoutQuotes2); // ๐Ÿ‘‰๏ธ ab c d e

The square brackets [] are called a character class and are used to match all characters between the brackets (double and single quotes).

The plus + matches the preceding item (the double and single quotes) 1 or more times.

In its entirety, the regular expression matches, one or more single or double quotes and replaces each match with an empty string to remove them.

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

# Remove the double quotes from a string using String.split()

This is a three-step process:

  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

remove double quotes from string using string split

The code for this article is available on GitHub

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

We split the string on each occurrence of a double quote.

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.

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array.

If a value for the separator argument is omitted, the array elements are joined with a comma ,.

If the separator argument is set to an empty string, the array elements are joined without any characters in between them.

You can also use a basic for loop to remove the double quotes from a string.

# Remove the double quotes from a string using a for loop

This is a four-step process:

  1. Declare a new variable that stores an empty string.
  2. Use a for loop to iterate over the original string.
  3. Check if the current character is not equal to a double quote.
  4. If the condition is met, concatenate the character to the new string.
index.js
const str = 'hel"l"o wor"l"d'; let newString = ''; for (let index = 0; index < str.length; index++) { if (str[index] !== '"') { newString += str[index]; } } console.log(newString); // ๐Ÿ‘‰๏ธ hello world

remove double quotes from string using for loop

The code for this article is available on GitHub

We used a basic for loop to iterate over the string.

On each iteration, we check if the current character is not equal to a double quote.

If the condition is met, we add the character to the new string.

If you need to remove the double and single quotes from a string, use the logical AND (&&) operator.

index.js
const str = 'a"b \'c "d \'e'; let newString = ''; for (let index = 0; index < str.length; index++) { if (str[index] !== '"' && str[index] !== "'") { newString += str[index]; } } console.log(newString); // ๐Ÿ‘‰๏ธ hello world

We used the logical AND (&&) operator, so for the if block to run, both conditions have to be met.

On each iteration, we check if the current character isn't equal to a double quote and isn't equal to a single quote.

If both conditions are met, we add the character to the new string.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev