Skip to main content

How to Remove Quotes from a String in JavaScript

Removing quotation marks from a string is a common data cleaning task. You might need to remove all quotes from a string for a database entry, or you might need to "unwrap" a string by removing only the quotes at the very beginning and end.

This guide will demonstrate the best modern JavaScript methods for both of these scenarios. You will learn how to use regular expressions with replace() and replaceAll() for maximum power and flexibility.

Removing All Quotes from a String

This is the most common use case: you want to find every instance of a quote character (single or double) and remove it, no matter where it appears in the string.

For example, you have a string with mixed quotes throughout, and you need to strip them all out.

// Problem: Remove all single and double quotes from this string.
const messyString = '"Hello", \'World\'! This is a "test".';

The String.prototype.replaceAll() method is a modern and highly readable way to replace all occurrences of a substring.

Solution: you can chain replaceAll() calls to remove both double and single quotes.

const messyString = '"Hello", \'World\'! This is a "test".';

// First, replace all double quotes, then replace all single quotes.
const cleanString = messyString.replaceAll('"', '').replaceAll("'", '');

console.log(cleanString);

Output:

Hello, World! This is a test.

The Classic Method: replace() with a Global Regex

Before replaceAll() was widely available, the standard method was to use String.prototype.replace() with a regular expression and the global (g) flag. This method is just as powerful and is still very common.

Solution: a regular expression character set ([...]) can be used to match multiple characters at once.

const messyString = '"Hello", \'World\'! This is a "test".';

// The regex /['"]/g finds all single (') or double (") quotes globally (g).
const cleanString = messyString.replace(/['"]/g, '');

console.log(cleanString);

Output:

Hello, World! This is a test.
note

This is often more concise than chaining replaceAll() calls, especially if you need to remove more than just two characters.

Removing Only Enclosing Quotes from a String

Sometimes, you don't want to remove all quotes, but only the ones that "wrap" the string at the beginning and end.

For example, you have a string that is enclosed in quotes, and you need to get the "unwrapped" content inside.

// Problem: Remove the outer quotes but not the inner ones.
const wrappedString = '"The file is named \'report.docx\'"';

The Core Method: Checking the Ends and Slicing

The most direct and readable way to do this is to check if the string starts and ends with a quote, and if so, use slice() to extract the content between them.

Solution:

function unwrapQuotes(str) {
// Check for enclosing double quotes
if (str.startsWith('"') && str.endsWith('"')) {
return str.slice(1, -1);
}
// Check for enclosing single quotes
if (str.startsWith("'") && str.endsWith("'")) {
return str.slice(1, -1);
}
// If no enclosing quotes, return the original string
return str;
}

// Example Usage:
const wrappedString1 = '"This is a test"';
const wrappedString2 = "'Another test'";
const innerQuoteString = "He said, \"Hello!\"";

console.log(unwrapQuotes(wrappedString1)); // Output: This is a test
console.log(unwrapQuotes(wrappedString2)); // Output: Another test
console.log(unwrapQuotes(innerQuoteString)); // Output: He said, "Hello!"
note

This str.slice(1, -1) extracts a "slice" of the string starting from the second character (index 1) and ending before the last character (index -1).

How the Regular Expression Works

The regular expression ['"]/g is a powerful and concise tool.

  • / ... /: These are the delimiters that mark the beginning and end of the regular expression pattern.
  • [...]: This is a character set. It tells the regex engine to match any single character that appears inside the brackets.
  • '" : Inside the character set, we list the characters we want to match: a single quote (') and a double quote (").
  • g: This is the global flag. It is crucial. It tells the replace() method to replace all matches it finds, not just the first one.

So, ['"]/g translates to: "Find every single quote and every double quote, wherever they appear in the string."

Conclusion

Removing quotes from strings in JavaScript is a simple task with the right modern methods.

  • To remove all occurrences of quotes, the most robust method is using replace() with a global regular expression: str.replace(/['"]/g, ''). The replaceAll() method is a great modern alternative.
  • To remove only the enclosing quotes, the best practice is to check the start and end of the string with startsWith() and endsWith() and then extract the middle part with slice(1, -1).