Look around and find out

Sometimes we want to see if something is followed by something else without actually including the second part.
That’s where lookaheads and lookbehinds come in.

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

What you’re looking for goes inside the parentheses.

For example:
\w+(?=\.pdf) - matches any word followed by .pdf (excluding the .pdf)

Sadly, not all browsers support lookbehinds, so we will focus on lookaheads for now

Task

Match only the prices, excluding the €

/ /
3x Apple - 3
2x Banana - 2
1x Orange - 1
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