How to remove quotes from a string in JavaScript

In this tutorial, let’s look at how to remove quotes from a string in JavaScript.

We’ll be looking at using methods that involve predefined string methods like replace, replaceAll, slice, substring, split and join to start with.

Then finally, we’ll end the tutorial by looking at manually removing the quotes with loops.

Using the replace method to remove quotes from a string in JavaScript

The easiest way to remove quotes from a string is by using the string replace method. This method can be used to replace a given substring (can be a single character or more) with another.

So, to remove double quotes, you can replace that double quote with an empty string. This serves as a neat trick to remove a character from a string, as shown in the below code snippet:

let str = '"Hello there", said John.';
str = str.replace('"','');
console.log(str); // Hello there", said John. 

But there’s an issue here. This method, by default, only removes/replaces the first occurrence of the given character/substring, as seen above. The second double quote remains in the string.

Let’s see how to remove both occurrences next.

Remove double quotes from a string with the replace method and regular expressions

The best way to remove every occurrence of a double quote is by using regular expressions.

Before we look at the entire expression, let’s look at how to use a regular expression in place of the example we used earlier. /”/ will essentially remove the first occurrence of a double quote in our string.

But regular expressions help us create more complicated conditions than that.

For instance, by using the global “g” flag, you can ask the ‘replace’ method to replace every occurrence of the double quote, as shown below:

str = str.replace(/"/g,'');
console.log(str); // Hello there, said John. 

Remove both double and single quotes with regular expressions

What if we want to remove both double and single quotes from a string?

In regular expressions, you can specify more than one character within square brackets, like this: /[“’]/g.

This way, every occurrence of either a single or double quote will be removed from the string on which this regular expression is applied, as shown in the snippet below:

let str = `"Hello there", said John. 'How are you?'`;
str = str.replace(/["']/g,'');
console.log(str); // Hello there, said John. How are you? 

Using more complicated regular expressions to remove quotes from a string

The beauty of regular expressions is that you can modify it to fit almost all your requirements. Unfortunately, it’s a huge topic that can’t be covered in its entirety in a single tutorial, so I’ll add just one more example.

What if you want to remove the double or single quotes from the beginning and end of the string, while leaving the rest intact?

You can modify the previous regular expression we used to achieve that.

Look at the code snippet below:

let str = `"Hello there 'hi'"`;
let newStr = str.replace(/^['"](.*)["']$/, '$1');
console.log(newStr); //Hello there 'hi' 

It might look a little confusing at first glance but let me explain it.

1. ^ refers to the start of the string. Right after it, we have [“’], meaning, we’re looking for a string that starts with either a single or double quote.
2. Then, we have .*, which indicates the rest of the characters of the string. * refers to 0 or more, so, right after the quote, the string can have (or not), any number of characters.
3. Finally, we have yet another [“’] that ends with $, indicating that we want our string to end with either a double or a single quote.
4. If these conditions are met, we’re replacing just those two quotes (at the beginning and end of the string) with nothing, effectively removing them.

Using the replaceAll method

If you just want to remove one type of quote (either single or double), you can forego using regular expressions, and instead use the replaceAll method.

This method can be used to remove every instance of a given substring or character, as shown in the example below:

let str = `"Hello there 'hi'"`;
let newStr = str.replaceAll('"','');
console.log(newStr); //Hello there 'hi' 

Unfortunately, the replaceAll method does not work with regular expressions, so you’re limited in `the conditions you can set.

Using the split and join methods

You can also use the split and join methods together to achieve the same result as the replaceAll method.

This is how it works:

1. We split the string into an array of elements. The “split” method needs a separator condition based on which the array elements are formed. Our condition is going to be the quote of our choice (double or single).
2. Then, we’re going to tag the “join” method next with an empty string as the join method.
That’s it! What does this do? While splitting with a quote as a condition, we’re removing every occurrence of that quote from the string.

Then, when we join the array with an empty string as the join condition, we join every other part of the string, except the quotes, and effectively remove them from our string, as shown below:

let str = `' Hi, how are you', said John.`;
let newStr = str.split("'").join('');
console.log(newStr); //Hi, how are you, said John. 

Using the slice method

You can use the slice method to slice off the first and last character of the string.

This method will work for our current purpose only when the quotes are at the beginning and end of the string, and you don’t want to remove any quotes in between.

let str = `"How are you, my dear?"`;
let newStr = str.slice(1,str.length-1);
console.log(newStr); //How are you, my dear? 

As shown in the example above, we’re slicing the string from the indices 1 through string_length – 1.

Every character in a string has a separate index that starts from 0 and ends with string_length – 1.

So, the first character has an index of 0, the second character has an index of 1, and so on, with the last character having an index of string_length – 1.

With the slice method, the first argument indicates the start of the slice, and second argument is not included in the slice.

So, 1 to length-1 will slice from the 2nd character (1st index) till the second last character (length – 2 index), effectively removing the quotes in the start and end of the string.

Use negative indices

The beauty of the “slice” method is that you can use negative indices in the 2nd argument to arrive at the same result.

-1 indicates the last character in a string, so we can slice from 1 through to -1, as shown below:

let newStr = str.slice(1,-1); 

Using the substring method

The “substring” method works similar to the “slice” method in that it removes a portion of a string based on the given positions, as shown below:

let newStr = str.substring(1,str.length-1); 

Negative indices don’t work with the substring method though, so you can’t give -1 as an argument here.

Manually remove quotes using loops

So far, we’ve used predefined JavaScript methods to remove quotes from a string.

But it’s always better to learn the manual way of doing things as well, just in case you forget the predefined methods, or they’re not supported in your browser.

In the following example, we’ve used the ‘for’ loop to remove every occurrence of both double and single quotes from a string.

1. We’ve created a new variable ‘newStr’ that’ll hold the newly created string (with the removed quotes). We’ve assigned it an empty string to start with.
2. Then, we’ve created a ‘for’ loop that iterates through the length of the string (0 till length – 1) .
3. For every iteration of the loop, we’re checking if the current character corresponding to the iteration is a single or a double quote (using the || operator).
4. If it is, we’re skipping that iteration using the ‘continue’ statement.
5. If not, we’re concatenating the current character to the string currently in the ‘newStr’ variable.
6. Finally, we’ve returned the ‘newStr’ variable.

let str = `"How are you, my dear?",asked John. 'Fine, and you?', asked Susan.`;

const removeQuotes = (str) => {
    let newStr = '';
    for(let i = 0; i < str.length; i++) {
        if(str[i] === '"' || str[i] === "'") {
            continue;
        }
        newStr += str[i];
    }
    return newStr;
}

console.log(removeQuotes(str)); //How are you, my dear?,asked John. Fine, and you?, asked Susan. 

That’s it! We’ve looked at how to remove quotes from a string in JavaScript in this tutorial. We’ve looked at using various predefined methods like replace, replace with regular expressions, replaceAll, split & join, slice and substring, and finally using the ‘for’ loop to manually remove every quote in the string.

Leave a Comment