Execute Program

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:
    truePass Icon
  • >
    /\./.test('Is that a cat?');
    Result:
    falsePass Icon
  • >
    /.\+./.test('111');
    Result:
    falsePass Icon
  • >
    /.\+./.test('1+1');
    Result:
    truePass Icon
  • >
    /\++/.test('+');
    Result:
    truePass Icon
  • >
    /\++/.test('++');
    Result:
    truePass Icon
  • >
    /\+\+/.test('++');
    Result:
    truePass Icon
  • >
    /\+\+/.test('+');
    Result:
    falsePass Icon
  • Escaping can be used when you need a literal ^, $, etc. (It works for any operator.)

  • >
    /\^/.test('^');
    Result:
    truePass Icon
  • >
    /\$/.test('$');
    Result:
    truePass Icon
  • >
    /\$$/.test('$a');
    Result:
    falsePass Icon
  • >
    /\$$/.test('a$');
    Result:
    truePass Icon
  • >
    /a|b/.test('|');
    Result:
    falsePass Icon
  • >
    /a\|b/.test('a|b');
    Result:
    truePass Icon