Skip to content
Allin

Regex Basics: A Beginner's Guide

Published 10/10/2025 · 3 min read · Developer tools

Daniel Okonkwo

Daniel OkonkwoFront-end developer and tech writer at Allin

Web performance · File formats

Checked against 2 sources

View profile
In short

A regular expression (regex) is a pattern for matching text. The essentials: literal characters match themselves, . matches any character, \d matches a digit, \w a word character, \s whitespace; * means zero or more, + one or more, ? optional; [abc] matches any listed character, ^ and $ anchor to the start and end. So \d{3}-\d{4} matches a phone number like 555-1234.

A regular expression is a pattern for matching text. Here are the building blocks — character classes, quantifiers and anchors — with a worked example.

What regex is for

A regular expression describes a pattern rather than an exact string, which lets you search, validate, extract and replace text far more flexibly. Instead of hunting for one fixed phone number, you describe the shape of any phone number and match them all. They power find-and-replace in editors, form validation, and pulling data out of logs and text.

The building blocks

Most of a pattern is literal text that matches itself. Where text varies, you use character classes: \d matches any digit, \w a word character (letters, digits, underscore), \s any whitespace, and the dot . matches almost any single character. A set in square brackets like [aeiou] matches any one character you list, and [^0-9] matches anything except those.

Quantifiers and anchors

Quantifiers say how many times the previous item may repeat: * is zero or more, + is one or more, ? is optional (zero or one), and {3} means exactly three, {2,4} between two and four. Anchors pin the match in place: ^ marks the start of the text and $ the end, so ^abc$ matches only the exact string 'abc'. Parentheses ( ) group parts together.

A worked example

To match a phone number like 555-1234, describe its shape: three digits, a hyphen, four digits. That is \d{3}-\d{4}. To also allow an optional area code in brackets, you might extend it step by step, testing as you go. Note that quantifiers are greedy by default — they grab as much as they can — which is worth remembering once patterns get longer.

Regex testerTest a regular expression and see what it does: matches highlighted in the text, every capture group listed, a replace panel, and the pattern read back to you piece by piece. Flags are toggles that say what they change, and a pattern that could freeze the page is flagged before you run it.Try the tool

Frequently asked questions

What does \d mean in regex?
It matches any single digit, 0 through 9. So \d{3} matches exactly three digits in a row.
What's the difference between * and +?
* matches zero or more of the preceding item, while + matches one or more — so + requires at least one occurrence.
Are regex the same in every language?
The core syntax is shared, but details and flavours differ between JavaScript, Python, PCRE and others — check your language's docs for edge cases.

Articles you may find interesting

All guides
ComparisonJSON vs XML: What's the Difference?JSON and XML both store structured data as text, but they trade off differently. Here's how each looks, where each wins, and how to choose.GuideFind and Replace: The Regex Features That Bite, Demonstrated One by OneGreedy against lazy on the same string, the dot that skips newlines, $& and $$ in the replacement, a reused /g regex that silently skips a row, and why /i knows nothing about Turkish i. Every failure run in Node, with a count-then-replace routine that catches them.ExplainerExtracting Every Email Address or URL from a Block of TextA URL at the end of a sentence keeps the full stop; an email address at the end of the same sentence does not. An accented first name in an address comes back truncated. Every case here was run through the tools and the exact output is reported.ExplainercamelCase vs snake_case: A Guide to Naming Conventions in CodecamelCase, snake_case, PascalCase, and kebab-case explained: what each looks like, where it is the convention, and how to choose one consistently.ExplainerEscaping a String for JSON: Three Characters Are Mandatory, and One Is a TrapRFC 8259 requires exactly three things to be escaped inside a JSON string. Everything else is optional. The one that actually breaks pipelines is a lone surrogate — legal in JSON text, impossible in UTF-8, and silently replaced the moment your data is written out.ExplainerXML to JSON: Attributes, Repetition, and the Single-Element Array TrapTwo documents that differ only in how many children exist produce two different JSON shapes, and no converter can tell them apart without a schema. Plus what this one really does with attributes, mixed content and whitespace — and the one thing it still cannot record.

Related tools

Sources

Spotted a mistake in this article?