What Is a Regular Expression? Visual Guide for Developers
A regular expression is a sequence of characters that defines a search pattern. You use it to find text that matches a specific shape: an email address, a ZIP code, a date in MM/DD/YYYY format, or any other structured string. Regular expressions are supported in nearly every programming language and most text editors.
Use the OnSumo Regex Visualizer to test any pattern from this article against your own input in real time.
How a regular expression works
A regex engine reads your pattern character by character and tries to find a substring in the target text that satisfies every rule in the pattern simultaneously. If the engine finds a match, it returns the position and length of the matched text. You can then extract that text, replace it, or simply confirm it exists.
In JavaScript, you write a regex literal between forward slashes: /hello/. That pattern matches the exact substring "hello" anywhere in the target string. Running "say hello world".match(/hello/) returns an array containing the matched string and its index.
Literal characters match themselves exactly. The power of regular expressions comes from special characters called metacharacters, each of which matches a class of characters rather than a single literal one.
The most common metacharacters
Dot (.) matches any single character except a newline. The pattern /c.t/ matches "cat", "cut", "c3t", and any other three-character sequence starting with c and ending with t.
Star (*) means zero or more of the preceding element. /ca*/ matches "c", "ca", "caa", "caaa", and so on.
Plus (+) means one or more. /ca+/ matches "ca" and longer, but not bare "c".
Question mark (?) means zero or one. /colou?r/ matches both "color" and "colour" because the "u" is optional.
Character classes ([]) match any one character from the listed set. /[aeiou]/ matches any vowel. /[a-z]/ matches any lowercase ASCII letter. /[^aeiou]/ matches any character that is NOT a vowel (the caret inside the class negates it).
Anchors and boundaries
Anchors do not match characters. They match positions in the string.
Caret (^) anchors the pattern to the start of the string (or the start of a line in multiline mode). /^hello/ matches strings that begin with "hello", not strings that contain "hello" somewhere in the middle.
Dollar sign ($) anchors to the end. /world$/ matches strings that end with "world".
Word boundary (\b) matches the position between a word character (letter, digit, or underscore) and a non-word character. /\bcat\b/ matches "cat" as a standalone word but not "catch" or "concatenate".
Groups and capturing
Parentheses create a group. Groups serve two purposes: they let you apply a quantifier to a sequence of characters, and they capture the matched text so you can extract it.
The pattern /(\d4)-(\d2)-(\d2)/ matches a date like "2026-09-15" and captures the year, month, and day into separate groups. In JavaScript, calling "2026-09-15".match(/(\d{4})-(\d{2})-(\d{2})/) returns an array where index 1 is "2026", index 2 is "09", and index 3 is "15".
Non-capturing groups use the syntax (?:...). They group without capturing, which is useful when you need to apply a quantifier to a sequence but do not need to extract the matched text separately.
Shorthand character classes
Most regex engines support shorthand sequences that stand in for common character classes.
\d matches any digit (0 through 9). Equivalent to [0-9].
\w matches any word character: letters, digits, and underscore. Equivalent to [A-Za-z0-9_].
\s matches any whitespace character: space, tab, newline, carriage return.
The uppercase versions negate each class: \D matches any non-digit, \W any non-word character, and \S any non-whitespace character.
Flags that change matching behavior
Flags modify how the engine applies the pattern. In JavaScript you append them after the closing slash: /pattern/flags.
g (global) finds all matches in the string instead of stopping at the first.
i (case-insensitive) treats uppercase and lowercase as equivalent. /hello/i matches "hello", "Hello", and "HELLO".
m (multiline) makes ^ and $ match the start and end of each line rather than the start and end of the entire string.
s (dotAll) makes the dot metacharacter also match newline characters, which it skips by default.
Try it in the visualizer
The best way to learn regular expressions is to write patterns against real input and watch the matches highlight in real time. The OnSumo Regex Visualizer runs in your browser without sending your text to a server. Paste your target string, type a pattern, and see matched groups color-coded immediately. It also shows the flags panel, a match list with positions, and group capture results.
Start with the examples above: paste a few date strings, apply /(\d{4})-(\d{2})-(\d{2})/g, and watch each date get matched and each group get captured. Then experiment with flags, negated classes, and anchors until the patterns become intuitive.
Reading a pattern one piece at a time
A regular expression looks like noise until you split it up. Take ^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$ and go left to right. The caret says start of the text. The first bracket group is a set of allowed characters, and the plus sign means one or more of them. Then a literal @. Then another set for the domain, an escaped dot, and finally two or more letters before the end of the line.
Once you read it like that, it stops being scary. Every regex is a short chain of small instructions. The trick is to never try to understand all of it at once.
That's why a visual tool helps. Paste the pattern, paste some sample text, and watch which parts match. When a group doesn't light up, you know exactly where to look.
The pieces you'll use most
You don't need the whole syntax. A handful of building blocks cover most real jobs.
- Literal characters match themselves. The pattern cat matches the word cat.
- A dot matches any single character except a line break.
- Square brackets match one character from a set, like [aeiou] or [0-9].
- Plus, star and question mark mean one or more, zero or more, and optional.
- Curly braces set exact counts, so \d{4} means four digits in a row.
- Parentheses make a group you can capture or repeat.
- A pipe means or. Mon|Tue matches either day.
- Anchors ^ and $ pin the match to the start and end of the line.
Greedy versus lazy matching
By default, quantifiers grab as much as they can. Run <.+> on the text <b>bold</b> and you get the whole string, not just the first tag. The match ran all the way to the last closing bracket.
Add a question mark and it turns lazy: <.+?> stops at the first possible end. This single detail causes more confusing bugs than anything else in regex, so learn it early.
Where regex goes wrong
Regex is great for patterns and bad for structure. Don't try to parse full HTML, nested brackets or a complete email standard with it. You'll end up with a monster that nobody can read, and it will still miss edge cases.
Watch out for catastrophic backtracking too. A pattern with nested repeats, like (a+)+$, can freeze on a long string that almost matches. If a pattern feels slow, simplify it before you blame the machine.
Test with text that should match and text that shouldn't. Most people only try the first kind, and that's how a loose pattern slips into production.
Flavors and flags
JavaScript, Python, PCRE and others differ slightly. Lookbehind support, named groups and Unicode handling are the usual trouble spots. If a pattern works in one place and fails in another, check the flavor before you rewrite it.
Flags change the rules too. The i flag ignores case, g finds every match instead of the first, m lets anchors work on each line, and s lets the dot cross line breaks. Your pattern and the text stay in your browser while you experiment.