Digits only
^\d+$
Matches: 12345
Rejects or skips: 123a
Use this reference when you know what a pattern must do but cannot remember a symbol. Each operator has a concrete example. Test a complete expression rather than memorizing isolated characters.
This reference is for checking syntax, not for following a complete course. Explore pattern collections or practice solving tasks to apply operators confidently.
| Concept | Regex | Meaning | Example input |
|---|---|---|---|
| Literal | cat | Finds exactly the sequence cat | catfish |
| Any character | c.t | The dot consumes one character, usually except a newline | cat |
| Character class | [aeiou] | Exactly one vowel | e |
| Negated class | [^0-9] | One non-digit | A |
| Digit | \d | A digit in the active engine | 5 |
| Word character | \w | Usually an ASCII letter, digit, or underscore in JavaScript | _ |
| Whitespace | \s | Whitespace such as a space or tab | |
| Optional | colou?r | The preceding character can occur zero or once | color |
| One or more | \d+ | One or more digits | 123 |
| Zero or more | ab*c | Zero or more b characters | ac |
| Exact count | [A-Z]{2} | Exactly two uppercase letters | BA |
| Range | \d{2,4} | Between two and four digits | 123 |
| Start | ^A | A at the start of the input (or line with m) | Apple |
| End | end$ | end at the end of the input (or line with m) | the end |
| Noncapturing | (?:cat|dog) | Group alternatives without an extra capture | dog |
| Named group | (?<code>[A-Z]{2}) | Capture a named value (JavaScript syntax) | BA |
| Positive lookahead | \d+(?= kg) | Match number only before kg | 12 kg |
| Positive lookbehind | (?<=ID:)\d+ | Match digits only after ID: | ID:42 |
| Escaped dot | \. | Match an actual dot, not any character | . |
| Alternation | cat|dog | Match either alternative | cat |
Examples describe JavaScript Regex. Other engines differ in Unicode classes, escape rules, lookbehind support and named-group syntax.
| Flag | When to use it |
|---|---|
g | Find all occurrences in a string, not just the first. |
i | Ignore case when uppercase and lowercase mean the same thing. |
m | Treat ^ and $ as line boundaries. |
s | Let the dot match newline characters. |
u | Use Unicode-aware pattern behavior; required for properties such as \p{L}. |
Run these examples and then deliberately alter a sample to discover a false positive. A format match does not prove an email exists or that a postal code is assigned.
^\d+$
Matches: 12345
Rejects or skips: 123a
^\d{5}-\d{3}$Matches: 40100-000
Rejects or skips: 4010-000
^ORD-[A-Z]{2}-\d{4}$Matches: ORD-BA-2026
Rejects or skips: ORD-ba-2026
{2,}Matches: Too many spaces
Rejects or skips: One space
^[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}$Matches: alex@example.com
Rejects or skips: alex@example
(?<code>[A-Z]{3}-\d{4})Matches: DEV-2026
Rejects or skips: DEV-26
. as a literal period: file.txt also matches fileXtxt. Write file\.txt.\d{5} finds five digits inside longer strings. Use ^\d{5}$ for a whole five-digit value..* for a known format: define explicit character classes and lengths, then test rejection cases.Practice method: write one valid case, one invalid case, choose a small pattern, then improve only after finding a counterexample.
Practice with eight documented mistakes and four realistic developer workflows. Compare the broken pattern, the fix, and negative test cases before you reuse a solution.
Open the debugging lab