JavaScript remove quotes from string | Example code

Use replace method with Regex to remove single to double quotes from string in JavaScript.

someStr.replace(/['"]+/g, '')

Remove Single or Double quotes from a string in JavaScript

HTML examples code.

Replace single and double quotes

<!DOCTYPE html>
<html>
<body>

  <script>
    var dq = 'He said "Hello, my name is Foo"';
    console.log(dq.replace(/['"]+/g, ''));

    var sq = "He said 'Hello, my name is Foo'";
    console.log(sq.replace(/['"]+/g, ''));
  </script>
</body>
</html>

Output:

JavaScript remove quotes from string
  • ['"] is a character class, matches both single and double-quotes. you can replace this with ” to only match double-quotes.
  • +: one or more quotes, chars, as defined by the preceding char-class (optional)
  • g: the global flag. This tells JS to apply the regex to the entire string. If you omit this, you’ll only replace a single char.

Remove the quotes around a given string

<!DOCTYPE html>
<html>
<body>

  <script>
    var str = 'remove "foo" delimiting double quotes';
    console.log(str.replace(/"([^"]+(?="))"/g, '$1'));

    str = 'remove only "foo" delimiting "';
    console.log(str.replace(/"([^"]+(?="))"/g, '$1')); //<-- trailing double quote is not removed

  </script>
</body>
</html>

Output:

Remove the quotes around a given string
  • ": literal, matches any literal: begin capturing group. Whatever is between the parentheses (()) will be captured and can be used in the replacement value.
  • [^”]+: Character class, matches all chars, except ” 1 or more times
  • (?=”): zero-width (as in not captured) positive lookahead assertion. The previous match will only be valid if it’s followed by a ” literal
  • ): end capturing group, we’ve captured everything in between the opening-closing "
  • ": another literal, cf list item one

Remove quotes beginning and end of the string

Double quotes

str.replace(/^"(.+(?="$))"$/, '$1');

Double and single quotes

str.replace(/^["'](.+(?=["']$))["']$/, '$1');

Complete code

<!DOCTYPE html>
<html>
<body>

  <script>
    var str = "'remove 'foo' delimiting double quotes'";
    console.log("Origianl : " + str);
    console.log(str.replace(/^["'](.+(?=["']$))["']$/, '$1'));

    str = '"remove only "foo" delimiting "';
    console.log("Origianl : " + str);
    console.log(str.replace(/^["'](.+(?=["']$))["']$/, '$1'));

  </script>
</body>
</html>

Output:

Remove quotes beginning and end of the string
  • ^": matches the beginning of the string ^ and a ". If the string does not start with a ", the expression already fails here, and nothing is replaced.
  • (.+(?="$)): matches (and captures) everything, including double-quotes one or more times, provided the positive lookahead is true
  • (?="$): the positive lookahead is much the same as above, only it specifies that the " must be the end of the string ($ === end)
  • "$: matches that ending quote, but does not capture it

If you’re not very comfortable using regex’s just yet, you might want to consider using:

var noQuotes = someStr.split('"').join('');

Source: stackoverflow.com

Do comment if you have any doubts and suggestions on this JS string topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.