Core syntax patterns

Match digits and words

/\d+/

Example:

/\w+/

Character classes

/[A-Z][a-z]+/

Example:

/[0-9A-F]{6}/

Quantifiers and anchors

Use quantifiers

/ab*/

Example:

/colou?r/

Start and end anchors

/^ERROR:/

Example:

/\.js$/

Practical validation snippets

Username pattern

/^[a-z0-9_]{3,16}$/

Example:

/^[A-Za-z][A-Za-z0-9_-]{2,20}$/

Simple date pattern

/^\d{4}-\d{2}-\d{2}$/

Example:

/^\d{2}\/\d{2}\/\d{4}$/

Common mistakes / Pitfalls

  • Assuming regex flavor is identical across JS, PCRE, and POSIX causes failures.
  • Greedy quantifiers can overmatch unless made lazy when needed.
  • For complex validation, regex alone may be insufficient without parser logic.
Last updated: February 2026