Regular Expressions: Escaping
Welcome to the Escaping lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
In most programming languages, strings are delimited with "double quotes". To put a literal double quote in a string, we escape it. The string
"\""contains exactly one double quote. (It's the same as'"'.)In regexes,
+normally repeats whatever comes before it. But we can escape the+as\+to match a literal "+". Likewise for other operators like..>
/\./.test('That is a cat.');Result:
true
>
/\./.test('Is that a cat?');Result:
false
>
/.\+./.test('111');Result:
false
>
/.\+./.test('1+1');Result:
true
>
/\++/.test('+');Result:
true
>
/\++/.test('++');Result:
true
>
/\+\+/.test('++');Result:
true
>
/\+\+/.test('+');Result:
false
Escaping can be used when you need a literal
^,$, etc. (It works for any operator.)>
/\^/.test('^');Result:
true
>
/\$/.test('$');Result:
true
>
/\$$/.test('$a');Result:
false
>
/\$$/.test('a$');Result:
true
>
/a|b/.test('|');Result:
false
>
/a\|b/.test('a|b');Result:
true