Saved

Utilix knowledge base

What Is Regex? A Guide to Regular Expressions

Published Apr 17, 2026

What Is Regex?

Regex (regular expressions) are sequences of characters that define a search pattern. They are supported in virtually every programming language and text editor, and are used for:

  • Searching — find all lines containing a date pattern
  • Validation — check that an email address is formatted correctly
  • Extraction — pull phone numbers out of a block of text
  • Replacement — reformat strings by capturing groups

Basic Syntax

PatternMeaningExample match
aLiteral character acat
.Any character except newlinec.t matches cat, cut
\dAny digit 0–9\d\d matches 42
\wWord character (letter, digit, underscore)\w+ matches hello_world
\sWhitespace (space, tab, newline)\s+ matches spaces
^Start of line^Hello
$End of lineworld$
[abc]Character class — a, b, or c[aeiou] matches vowels
[^abc]Negated class — anything except a, b, c[^0-9] matches non-digits

Quantifiers

QuantifierMeaning
*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times

Example: \d{4}-\d{2}-\d{2} matches a date like 2026-04-18.

Groups and Alternation

  • (abc) — capturing group; the match is remembered for back-references or extraction
  • (?:abc) — non-capturing group; groups without remembering
  • a|b — alternation; matches a or b

Example: (Mr|Ms|Mrs)\.?\s+\w+ matches Mr Smith, Ms. Jones, Mrs Brown.

Flags (Modifiers)

Most regex engines support flags after the closing delimiter:

FlagEffect
iCase-insensitive
gGlobal — find all matches, not just the first
mMultiline — ^ and $ match line start/end
sDot-all — . also matches newline

Common Patterns

Email:       ^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$
URL:         https?://[\w./?=&%-]+
IPv4:        \b(\d{1,3}\.){3}\d{1,3}\b
ISO Date:    \d{4}-\d{2}-\d{2}
Hex colour:  #[0-9a-fA-F]{3,6}
Phone (UK):  (?:0|\+44)\d{9,10}

Greedy vs Lazy Matching

By default, quantifiers are greedy — they match as much text as possible.

Input:  <b>bold</b> and <i>italic</i>
Greedy:    <.*>   →  <b>bold</b> and <i>italic</i>  (entire string)
Lazy:      <.*?>  →  <b>  (stops at first >)

Add ? after a quantifier to make it lazy: *?, +?, {n,m}?.

Regex in JavaScript

const pattern = /\d{4}-\d{2}-\d{2}/g;
const text = "Events: 2026-04-18, 2026-05-01";
const matches = text.match(pattern);
// ["2026-04-18", "2026-05-01"]

Use the Regex Tester to build and test patterns against your own text with live match highlighting.