Do it again

You already learned about groups.

I like (apples|bananas) matches “I like apples” and “I like bananas”
(ha)+ matches “haha” and “hahahaha”

But what if you want to match the same group again?
That’s where group references come in.

You can use \1, \2, \3, etc. to refer to the first, second, third, etc. group in the regex.

For example:
(Finn|Kai) is the same Person as \1

The reference will only match the same text as the group it refers to.
So Finn is the same Person as Kai will not match

Task

Match Apples & Apples and Bananas & Bananas

/ /
Apples & Apples
Bananas & Bananas
Oranges & Oranges
Apples & Bananas
Regex Cheatsheet

Here you find every Regex character explained

Anchors

  • ^ - Start of a string
  • $ - End of a string
  • \b - Word boundary
  • \B - Not word boundary

Quantifiers

  • ? - Optional
  • + - 1 or more times
  • * - 0 or more times
  • {n} - Exactly n times
  • {n,} - at least n times
  • {,m} - at most m times
  • {n,m} - Between n and m times

Character Classes

  • [abc] - Character Set
  • [^abc] - Not in character set
  • [a-z] - Range of characters
  • . - Any character except newline
  • \d - Digit
  • \D - Not a digit
  • \w - Word character
  • \W - Not a word character
  • \s - Whitespace
  • \S - Not whitespace

Groups

  • (abc) - Capturing group
  • (?:abc) - Non-capturing group
  • \1 - Group Reference

Lookarounds

  • (?=abc) - Positive lookahead
  • (?!abc) - Negative lookahead
  • (?<=abc) - Positive lookbehind
  • (?<!abc) - Negative lookbehind

Flags

  • g - Global
  • i - Case-insensitive
  • m - Multiline

Other Characters

  • \[Symbol] - Use a Regex Symbol as Text
  • | - Or