Programmingbeginner14 min read

Regular Expressions: A Practical Guide for Developers

Learn regex from the ground up: character classes, quantifiers, groups, lookaheads, and a library of ready-to-use patterns for everyday developer tasks.

Why Regex Exists

Regular expressions are a concise language for describing patterns in text. Developers use them daily for:

  • Input validation — checking that an email address, phone number, or URL matches the expected format
  • Log parsing — extracting fields from structured log lines without a full parser
  • Bulk find-and-replace — renaming variables, updating import paths, or transforming data files
  • Text extraction — pulling specific values from HTML, Markdown, or configuration files

Once you understand the core concepts, regex replaces dozens of lines of string-manipulation code with a single, expressive pattern.

Character Classes and Quantifiers

Character classes match a single character from a defined set:

  • `[abc]` — matches `a`, `b`, or `c`
  • `[a-z]` — any lowercase letter
  • `[^abc]` — any character except `a`, `b`, or `c`
  • `\d` — any digit (`[0-9]`)
  • `\w` — any word character (`[a-zA-Z0-9_]`)
  • `\s` — any whitespace (space, tab, newline)
  • `.` — any character except newline

Quantifiers control how many times a token can repeat:

  • `*` — zero or more
  • `+` — one or more
  • `?` — zero or one (optional)
  • `{3}` — exactly 3
  • `{2,5}` — between 2 and 5
  • `{3,}` — 3 or more

Quantifiers are greedy by default — they match as much as possible. Add `?` after a quantifier to make it lazy (match as little as possible): `.*?`

// Match a 4-digit year
/\d{4}/

// Match one or more word characters (a 'word')
/\w+/

// Match an optional decimal part
/\d+(\.\d+)?/

// Match any amount of whitespace (lazy)
/.+?/

Anchors, Groups, and Alternation

Anchors match a position rather than a character:

  • `^` — start of string (or line in multiline mode)
  • `$` — end of string (or line)
  • `\b` — word boundary (between `\w` and `\W`)

Groups capture matched text for later use:

  • `(abc)` — capturing group; the match is stored and can be referenced as `$1`, `\1`, or via `match[1]`
  • `(?:abc)` — non-capturing group; groups tokens for quantifiers without storing the match
  • `(?<name>abc)` — named capturing group; reference with `match.groups.name`

Alternation (`|`) works like OR: `cat|dog` matches either `cat` or `dog`. Use groups to scope it: `(cat|dog)s` matches `cats` or `dogs`.

const datePattern = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/;
const match = '2024-05-31'.match(datePattern);
console.log(match.groups);  // { year: '2024', month: '05', day: '31' }

Lookaheads and Lookbehinds

Zero-width assertions match a position based on surrounding context without consuming characters:

  • `(?=...)` — positive lookahead: position must be followed by the pattern
  • `(?!...)` — negative lookahead: position must NOT be followed by the pattern
  • `(?<=...)` — positive lookbehind: position must be preceded by the pattern
  • `(?<!...)` — negative lookbehind: position must NOT be preceded by the pattern

Example: match a number only when followed by `px`:

// Match numbers followed by 'px'
/\d+(?=px)/

'font-size: 14px'.match(/\d+(?=px)/);  // → ['14']
'margin: 10em'.match(/\d+(?=px)/);     // → null

// Match price amounts preceded by '$'
/(?<=\$)\d+(\.\d{2})?/

'Total: $29.99'.match(/(?<=\$)\d+(\.\d{2})?/);  // → ['29.99']

Ready-to-Use Patterns

Patterns every developer should have handy:

// Email (RFC 5322 simplified)
/^[^\s@]+@[^\s@]+\.[^\s@]+$/

// URL (http and https)
/^https?:\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/

// IPv4 address
/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/

// Hex color (#RGB or #RRGGBB)
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/

// Semantic version (e.g. 1.2.3)
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/

// ISO 8601 date (YYYY-MM-DD)
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/

// UUID v4
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

Testing Regex Live with the DevForge Regex Tester

The fastest way to build a regex is to test it against real input in real time. The DevForge Regex Tester lets you type a pattern and instantly see matches highlighted in your test string — no browser console, no Node.js script required.

It also shows captured groups separately, so you can verify that your named groups are extracting exactly the data you need. This is particularly useful when writing patterns for log parsing or form validation.

Try it on DevForge

Free online tools related to this tutorial — no signup required.

Related Tutorials