Last updated: Mar 1, 2024
Reading timeยท6 min
String.replace()
String.split()
for
loopUse 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.
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
If you only need to remove the single quotes from a string, use the following code sample.
// โ 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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
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.
If you need to remove only the enclosing quotes from a string, use the
String.slice()
method.
// โ 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
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.
// โ 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.
String.replace()
This is a three-step process:
replace()
method on the string.replace
method will replace each occurrence of a quote with an empty
string.replace
method will return a new string with all quotes removed.// โ 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
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
The two forward slashes / /
mark the beginning and end of the regular
expression.
const str = 'hel"l"o wor"l"d'; const withoutQuotes = str.replace(/"/g, ''); console.log(withoutQuotes); // ๐๏ธ hello world
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.
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.
String.split()
This is a three-step process:
String.split()
method to split the string on each double quote.join()
method to join the array of strings.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.
We split the string on each occurrence of a double quote.
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 ,
.
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.
for
loopThis is a four-step process:
for
loop to iterate over the original string.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
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.
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.
You can learn more about the related topics by checking out the following tutorials: