OnSumo Tools

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.