Reference library

Regular expressions: syntax and examples

Character classes, quantifiers, groups, assertions, and JavaScript RegExp flags with matching examples.

JavaScript RegExp

Pattern, match, and purpose

Click an expression to copy the complete /pattern/flags literal. Highlighting shows the part of the sample that matches.

JavaScript regular expressions with matches and explanations
Expression Input and match When to use
Characters and classesLiteral text, any character, and sets of allowed characters. 6
black cat
Literal text Matches a sequence of characters exactly as written.
cat, cut
Any character A dot matches one character, except a line terminator unless s is enabled.
Anna and Bob
Class and range Square brackets define one allowed character; a hyphen creates a range.
123 apples
Negated class A ^ immediately after [ matches anything except the listed characters.
Order 42 ready
Shorthand classes \d is a digit, \s is whitespace, and \w is an ASCII letter, digit, or _.
Привет, hello
Unicode property \p{L} matches letters from any script when Unicode mode is enabled. Requires u
QuantifiersHow many times the preceding character, class, or group may repeat. 6
ac, abbc
Zero or more: * The preceding item may be absent or repeat any number of times.
ac, abbc
One or more: + The preceding item must occur at least once.
color, colour
Optional item: ? The preceding item occurs zero or one time.
Year: 2026
Exact count: {n} Curly braces set an exact number of repetitions.
7, 42, 2026
Count range: {n,m} Limits the minimum and maximum repetition count.
<b>text</b>
Lazy quantifier A ? after a quantifier selects the shortest possible fragment. Without ? this is one match
Boundaries and groupsStarts and ends, alternatives, and combining parts of a pattern. 6
Error: file not found
Start anchor: ^ Requires a match at the start of the input, or a line when m is enabled.
Task done
End anchor: $ Requires a match at the end of the input, or a line when m is enabled.
cat, catalog
Word boundary: \b Separates an ASCII word token from adjacent letters, digits, and underscores.
cat or dog
Alternation: | A vertical bar allows one of several alternatives.
Date: 2026-07-21
Capturing group Parentheses group a pattern and store the matched parts.
https://example.com
Non-capturing group (?:…) groups part of a pattern without adding a captured result.
Assertions and referencesConditions around a match and reuse of captured text. 4
Price: 120
Positive lookahead (?=…) requires following text without including it in the match.
foo, foobar
Negative lookahead (?!…) rejects a specified continuation on the right.
#topic
Positive lookbehind (?<=…) requires preceding text without including it in the match.
a very very useful rule
Named backreference Stores a group by name and matches the same text again.
FlagsSearch modes written after the closing slash. 5
cat, cat, cat
All matches: g Continues searching after the first match.
Textria
Ignore case: i Matches uppercase and lowercase forms.
TODO: review
done
TODO: send
Multiline mode: m Makes ^ and $ work at the boundaries of every line.
start
new line
end
Dot includes newlines: s Allows the dot to match line terminators.
💡
Unicode mode: u Treats a full code point as one character and enables \p{…}.
Quick notes

Build predictable patterns

Escape metacharacters
Use a backslash before . * + ? ( ) [ ] { } ^ $ | \ when it must be literal.
Choose flags deliberately
g changes one match into many; m changes anchors; s changes the dot; u enables Unicode-aware behavior.
Keep patterns narrow
Prefer explicit classes and limits over a broad .* when the input format is known.
Test edge cases
Check empty input, line breaks, non-Latin text, long strings, and almost-matching values.

Syntax checked against the ECMAScript RegExp specification. Other regular-expression engines may differ.

Open the online notebook
Related tools

Continue working with text

Copy a symbol, convert markup, clean a fragment, or keep the result in the notebook.

FAQ

Questions about regular expressions

Why is this reference specific to JavaScript?

Regular-expression engines differ in syntax and behavior. This page uses ECMAScript RegExp literals, flags, Unicode property escapes, named groups, and lookarounds.

What do the slashes mean?

In /pattern/flags the slashes delimit a JavaScript RegExp literal. When using new RegExp(), pass the pattern and flags as strings instead and account for string escaping.

Why does .* select too much?

Quantifiers are greedy by default. Add ? to make a quantifier lazy, or use a narrower character class that cannot cross the intended boundary.

Can a regular expression be slow?

Yes. Ambiguous nested quantifiers can cause heavy backtracking on long input. Keep alternatives distinct, constrain repetitions, and test patterns against worst-case strings.