Word Boundaries

You already now the beginning ^ and the end $ anchors.
They match either the beginning or the end of a text or line (with the m flag).

But if you want to anchor to a word boundary?
That’s where the \b anchor comes in.

The \b anchor matches the position between a word character and a non-word character.

For example:
\ba matches every word that starts with an a
a\b matches every word that ends with an a

The opposite also exists:
\B matches the position between two characters that are not a word boundary

Task

Match the first and last character of every word

/ /
Hi there, how are you?
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