How do you escape a quote in javascript?

JavaScript:

     /**
 *
 * @param {type} param1
 * @param {type} param2
 * @param {type} param3
 */
jQuery.validator.addMethod('commaSeparatedText', function(value, element) {

    if (value.length === 0) {
        return true;
    }
    var expression = new RegExp("^((')([^\'\\\\]*(?:\\\\.[^\'\\\\])*)[\\w\\s,\\.\\-_\\[\\]\\)\\(]+([^\'\\\\]*(?:\\\\.[^\'\\\\])*)('))(((,)|(,\\s))(')([^\'\\\\]*(?:\\\\.[^\'\\\\])*)[\\w\\s,\\.\\-_\\[\\]\\)\\(]+([^\'\\\\]*(?:\\\\.[^\'\\\\])*)('))*$");
    return expression.test(value);
}, 'Invalid comma separated string values.');

Escape Quotes in a String #

To escape a single or double quote in a string, use a backslash \ character before each single or double quote in the contents of the string, e.g. 'that\'s it'.

Copied!

const escapeSingle = 'it\'s a string'; console.log(escapeSingle) // 👉️ it's a string

The backslash character allows us to escape the single quote, so it's interpreted as the literal single quote character, and not as an end of string character.

You can use the same approach to escape a double quote in a string.

Copied!

const escapeDouble = "He said: \"test 123\"" console.log(escapeDouble) // 👉️ He said: "test 123"

We use the backslash \ character to escape each double quote in the string.

Escaping a quote can be avoided by changing the outer quotes of the string.

Copied!

const withSingle = "it's a string"; console.log(withSingle) // 👉️ it's a string const withDouble = 'He said: "test 123"' console.log(withDouble) // 👉️ He said: "test 123"

We alternate between double and single quotes, so we don't have to escape them.

Note that you can also use backticks as outer quotes for a string. This allows you to use both single and double quotes in the string without having to escape them.

Copied!

const withBoth = `it's a "test 123"`; console.log(withBoth) // 👉️ it's a "test 123"

The outer quotes of the string use backticks so we don't have to escape the single or double quotes in the string.

To add a backslash \ character to a string, add two backslashes next to one another. The first backslash escapes the second, so the second is taken literally.

Copied!

const addBackslash = "He said: \\\"test 123\\\"" console.log(addBackslash) // 👉️ He said: \"test 123\"

We have 3 backslashes next to one another. The first backslash escapes the second, so it is interpreted literally by JavaScript. The third backslash is used to escape the double quotes.

Here's a more realistic example, where we only add a backslash to the string.

Copied!

const addBackslash = "BMW \\1996\\" console.log(addBackslash) // 👉️ BMW \1996\

Further Reading #

  • Check if String contains any Letter in JavaScript
  • Check if String contains Special Characters in JavaScript
  • Check if String starts with Substring in JavaScript
  • Check if String ends with Substring in JavaScript
  • Check if String starts with one of Multiple Values in JS
  • How to count the words in a String in JavaScript
  • Check if String contains only Latin Letters in JavaScript
  • Check if a String is all Uppercase in JavaScript
  • Check if First Letter of String is Uppercase in JavaScript
  • Get first letter of each Word in a String in JavaScript
  • Remove the Last Word from a String using JavaScript

How do you escape a quote?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

How do I escape a single quote?

Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.

How do you escape a single quote from a string?

You need to escape single quote when the literal is enclosed in single code using the backslash(\) or need to escape double quotes when the literal is enclosed in a double code using a backslash(\).

How do you escape a special character in JavaScript?

JavaScript uses the \(backslash) as an escape characters for:.
\' single quote..
\" double quote..
\ backslash..
\n new line..
\r carriage return..
\t tab..
\b backspace..
\f form feed..

Please find in the below code which escapes the single quotes as part of the entered string using a regular expression. It validates if the user-entered string is comma-separated and at the same time it even escapes any single quote(s) entered as part of the string.

In order to escape single quotes, just enter a backward slash followed by a single quote like: \’ as part of the string. I used jQuery validator for this example, and you can use as per your convenience.

Valid String Examples:

'Hello'

'Hello', 'World'

'Hello','World'

'Hello','World',' '

'It\'s my world', 'Can\'t enjoy this without me.', 'Welcome, Guest'

HTML: