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

Regular expressions – my secret love

stefan judis
December 03, 2019

Regular expressions – my secret love

stefan judis

December 03, 2019
Tweet

More Decks by stefan judis

Other Decks in Technology

Transcript

  1. /^(Hello|Bonjour) dotJS!(.+)$/ Match a string that starts with "Hello" or

    "Bonjour" followed by " dotJS!", capture more than one character...
  2. /^(Hello|Bonjour) dotJS!(.+)$/ Match a string that starts with "Hello" or

    "Bonjour" followed by " dotJS!", capture more than one character until the end.
  3. /^(Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: "Hello", 2: " How are you?" ]
  4. /^(Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: "Hello", 2: " How are you doing?" ]
  5. /^(Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: "Hello", 2: " How are you?" ]
  6. /^(Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: "Hello", 2: " How are you?" ]
  7. /^(Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: "Hello", 2: " How are you?" ]
  8. /^(?:Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: " How are you?" ]
  9. /^(?:Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: " How are you?" ] (?: ...) – there are non-capturing groups in JavaScript
  10. /^(?:Hello|Bonjour) dotJS!(.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: " How are you?" ]
  11. /^(?:Hello|Bonjour) dotJS!(?<rest>.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: " How are you?", groups: { rest: " How are you?" } ]
  12. /^(?:Hello|Bonjour) dotJS!(?<rest>.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: " How are you?", groups: { rest: " How are you?" } ]
  13. /^(?:Hello|Bonjour) dotJS!(?<rest>.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you doing?", 1: " How are you doing?", groups: { rest: " How are you?" } ] (?<name> ...) – there are named capture groups in JavaScript
  14. /^(?:Hello|Bonjour) dotJS!(?<rest>.+)$/ .exec( "Hello dotJS! How are you?" ) [

    0: "Hello dotJS! How are you?", 1: " How are you doing?", groups: { rest: " How are you?" } ]
  15. /^(Bonjour) \1 dotJS!(?<rest>.+)$/ .exec( "Bonjour Bonjour dotJS! How are you?"

    ) [ 0: "Hello dotJS! How are you?", 1: "Bonjour" 2: " How are you?", groups: { rest: " How are you?" } ]
  16. /^(Bonjour) \1 dotJS!(?<rest>.+)$/ .exec( "Bonjour Bonjour dotJS! How are you?"

    ) [ 0: "Hello dotJS! How are you doing?", 1: "Bonjour" 2: " How are you?", groups: { rest: " How are you?" } ] \1, \2, ... – there are capture group back references in JavaScript
  17. /^(Bonjour) \1 dotJS!(?<rest>.+)$/ .exec( "Bonjour Bonjour dotJS! How are you?"

    ) [ 0: "Hello dotJS! How are you?", 1: "Bonjour" 2: " How are you?", groups: { rest: " How are you?" } ]
  18. /^(?:Bonjour) dotJS!(?<rest>.+) \k<rest>$/ .exec( "Bonjour dotJS! How are you? How

    are you?" ) [ 0: "Bonjour dotJS! How are you? How are you?", 1: " How are you?", groups: { rest: " How are you?" } ]
  19. /^(?:Bonjour) dotJS!(?<rest>.+) \k<rest>$/ .exec( "Bonjour dotJS! How are you? How

    are you?" ) [ 0: "Bonjour dotJS! How are you? How are you?", 1: " How are you?", groups: { rest: " How are you?" } ] \k<name> – there are named capture group back references in JavaScript
  20. /^(?:Bonjour) dotJS!(?<rest>.+)$/ .exec( "Bonjour dotJS! How are you?" ) [

    0: "Bonjour dotJS! How are you?", 1: " How are you?", groups: { rest: " How are you?" } ]
  21. /^(?:Bonjour) dotJS!(?<rest>.+)$/s .exec( "Bonjour dotJS! How are\nyou?" ) [ 0:

    "Bonjour dotJS! How are↵you?", 1: " How are↵you?", groups: { rest: " How are↵you?" } ]
  22. /^(?:Bonjour) dotJS!(?<rest>.+)$/s .exec( "Bonjour dotJS! How are\nyou?" ) [ 0:

    "Bonjour dotJS! How are↵you?", 1: " How are↵you?", groups: { rest: " How are↵you?" } ] / .../s – the dotall flag really captures every character
  23. /^(?:Bonjour) dotJS!(?<rest>.+)\p{Emoji}$/su .exec( "Bonjour dotJS! How are\nyou?" ) [ 0:

    "Bonjour dotJS! How are↵you?", 1: " How are↵you?", groups: { rest: " How are↵you?" } ]
  24. /^(?:Bonjour) dotJS!(?<rest>.+)\p{Math_Symbol}$/su .exec( "Bonjour dotJS! How are\nyou?⁼" ) [ 0:

    "Bonjour dotJS! How are↵you⁼?", 1: " How are↵you?", groups: { rest: " How are↵you?" } ]
  25. /^(?:Bonjour) dotJS!(?<rest>.+)\p{Script=Greek}$/su .exec( "Bonjour dotJS! How are\nyou?μ" ) [ 0:

    "Bonjour dotJS! How are↵youμ?", 1: " How are↵you?", groups: { rest: " How are↵you?" } ]
  26. /^(?:Bonjour) dotJS!(?<rest>.+)\p{Script=Greek}$/su .exec( "Bonjour dotJS! How are\nyou?μ" ) [ 0:

    "Bonjour dotJS! How are↵youμ?", 1: " How are↵you?", groups: { rest: " How are↵you?" } ] p/{ ...} – unicode property escapes are a thing these days!
  27. Other exciting "new" features to check out lookahead and lookbehind

    assertions String.prototype.matchAll string replacement patterns