Utility Hub
Developer

Regular Expressions Explained for Beginners

A beginner's guide to regular expressions — what they are, how the syntax works, and practical examples using JavaScript-compatible patterns.

RegexRegular ExpressionsDeveloper Tools

What is a regular expression?

A regular expression (often shortened to regex or regexp) is a pattern that describes a set of strings. Give a regex engine some text and a pattern, and it tells you whether the pattern appears in the text — and where.

Regex is built into virtually every programming language and many text editors. It is used for searching, validation, extraction, and substitution. The syntax looks cryptic at first, but it follows consistent rules.

Literals

The simplest regex is a literal string. The pattern cat matches the sequence of characters c, a, t wherever it appears:

Text:    "The cat sat on the mat."
Pattern: cat
Match:   "The [cat] sat on the mat."

Regex is case-sensitive by default. cat does not match Cat unless you use the case-insensitive flag.

Character classes

A character class matches one character from a set. You define the set inside square brackets [ ].

PatternMatches
[aeiou]Any single vowel
[0-9]Any single digit
[a-z]Any lowercase letter
[A-Za-z]Any letter (upper or lower)
[^aeiou]Any character that is not a vowel

Ranges work within a character class: [a-z] matches any character from a to z.

Shorthand character classes — these are built-in shortcuts:

ShorthandEquivalentMeaning
\d[0-9]Any digit
\D[^0-9]Any non-digit
\w[A-Za-z0-9_]Any word character
\W[^A-Za-z0-9_]Any non-word character
\s[ \t\n\r]Any whitespace character
\S[^ \t\n\r]Any non-whitespace character
.(everything except newline)Any single character

Quantifiers

Quantifiers specify how many times the preceding element must appear.

QuantifierMeaning
?Zero or one time (optional)
*Zero or more times
+One or more times
{n}Exactly n times
{n,}At least n times
{n,m}Between n and m times

Examples:

\d+       matches one or more digits: "42", "007", "1"
\d{4}     matches exactly four digits: "2024"
colou?r   matches "color" and "colour" (the 'u' is optional)

By default, quantifiers are greedy — they match as much as possible. Add ? after a quantifier to make it lazy (match as little as possible): .*? vs .*.

Anchors

Anchors do not match characters — they match positions in the string.

AnchorMatches
^Start of the string (or start of a line in multiline mode)
$End of the string (or end of a line in multiline mode)
\bA word boundary — the position between a word character and a non-word character
^Hello    matches "Hello" only at the start of the string
world$    matches "world" only at the end of the string
\bcat\b   matches "cat" as a whole word, not "scattered" or "concatenate"

Groups

Parentheses () create a capturing group — they group part of the pattern and capture the matched text for later use.

(\d{4})-(\d{2})-(\d{2})

This matches a date like "2024-03-15" and captures the year, month, and day as three separate groups.

Non-capturing groups use (?:...) — they group without capturing:

(?:https?|ftp)://

This matches http://, https://, or ftp:// without creating a capture group.

Alternation (|) inside a group works like OR:

gr(a|e)y    matches "gray" and "grey"

Flags

Flags modify how the pattern is applied. In JavaScript, they are appended after the closing slash of a regex literal.

FlagDescription
iCase-insensitive matching
gGlobal — find all matches, not just the first
mMultiline — ^ and $ match line starts/ends
sDotall — . matches newline characters too
// JavaScript examples
/cat/i        // matches "cat", "Cat", "CAT"
/\d+/g        // find all numbers in the string
/^start/m     // match "start" at the beginning of any line

Putting it together — simple examples

Match a 5-digit zip code:

^\d{5}$

Match a hex colour (like #3a7bd5):

#[0-9a-fA-F]{6}

Extract words that start with a capital letter:

\b[A-Z][a-z]+\b

Match a time in HH:MM format:

^([01]\d|2[0-3]):[0-5]\d$

How to test regex

The best way to learn regex is to experiment. Paste a pattern and some text into the Regex Tester, and you can see matches highlighted in real time. Adjust the pattern, try edge cases, and build your intuition from working examples.

Also see: Common Regex Patterns and Examples for ready-to-use patterns you can adapt.