Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Regular Expressions in Javascript

Regular Expressions in Javascript

An overview of regular expressions in Javascript. Intended as a supplement to Eloquent Javascript ch.9 by Marijn Haverbeke.

Fen Slattery

February 20, 2017
Tweet

More Decks by Fen Slattery

Other Decks in Programming

Transcript

  1. Constructor new RegExp('ab+c', 'i'); new RegExp(/ab+c/, 'i'); Don't forget the

    normal string escape rules for special characters!
  2. Brackets [abc] - Matches any single character a, b, or

    c [^abc] - Matches any character except a, b, c [a-c] - Matches any character from a to c
  3. Character Classes \w - Alphanumeric (word) character \W - Non-alphanumeric

    character \s - Whitespace character \S - Non-whitespace character
  4. Boundaries ^ - Start of line $ - End of

    line \b - At beginning or end of word \B - Not at beginning or end of word
  5. Quantifiers n+ - Match one or more n n* -

    Match zero or more n's n? - Match zero or one n's
  6. Quantifiers n{3} - Match sequence of 3 n's n{3,5} -

    Match sequence of 3 to 5 n's n{3,} - Match sequence of 3 or more n's
  7. Assertions x(?=y) - Matches x only if x is followed

    by y x(?!y) - Matches x only if x is not followed by y
  8. Greedy Operators + * ? {} /<.+>/.exec("<p>Hello world</p>"); => "<p>Hello

    world</p>" /<.+?>/.exec("<p>Hello world</p>"); => "<p>"