a, 5, !).., *, ^).() used to capture a substring for later use or back-referencing.In many languages (like JavaScript), regex patterns are defined between slashes, followed by flags.
/pattern/flags
g: Global search. Finds all matches in the string, not just the first one.i: Case-insensitive. Matches both uppercase and lowercase letters.m: Multiline mode. Allows anchors (^, $) to match the start/end of lines, not just the whole string.Example: /hello/gi would find all occurrences of "hello", "Hello", "HELLO", etc., in a string.
| Character | Description | Example |
|---|---|---|
^ | Asserts position at the start of the string (or line in multiline mode). | ^A matches "A" in "Apple" |
$ | Asserts position at the end of the string (or line in multiline mode). | e$ matches "e" in "Apple" |
\b | Word boundary. Matches a position between a word and a non-word character. | \bcat\b matches "cat" in "The cat sat." but not in "caterpillar". |
| Character | Description |
|---|---|
. | Matches any single character except newline. |
\d | Matches any digit (0-9). Equivalent to [0-9]. |
\D | Matches any non-digit. |
\w | Matches any word character (alphanumeric + underscore). Equivalent to [A-Za-z0-9_]. |
\W | Matches any non-word character. |
\s | Matches any whitespace character (space, tab, newline). |
\S | Matches any non-whitespace character. |
[...] | Character set. Matches any one of the characters inside the brackets (e.g., [aeiou]). |
[^...] | Negated character set. Matches any character not inside the brackets. |
| Quantifier | Description |
|---|---|
* | Matches the preceding item 0 or more times. |
+ | Matches the preceding item 1 or more times. |
? | Matches the preceding item 0 or 1 time. (Makes it optional). |
{n} | Matches the preceding item exactly n times. |
{n,} | Matches the preceding item n or more times. |
{n,m} | Matches the preceding item between n and m times. |
| Syntax | Description |
|---|---|
(...) | Grouping. Groups multiple tokens together and creates a capture group. |
(?:...) | Non-capturing group. Groups tokens but does not create a capture group. |
| | Alternation (OR). Matches either the expression before or after the pipe. |
5 with \d).\d+ for one or more digits).^, $) and word boundaries (\b) to prevent partial matches.? to make them "lazy" (match as little as possible). For example, to match an HTML tag: <.*?> is often better than <.*>.This table summarizes the most common tokens.
| Token | Meaning | Token | Meaning |
|---|---|---|---|
. | Any character | \d | Digit |
\w | Word character | \s | Whitespace |
[...] | Character set | ^ | Start of string |
$ | End of string | * | 0 or more |
+ | 1 or more | ? | 0 or 1 |
(...) | Capture group | | | OR |
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
^: Start of the string.[^\s@]+: One or more characters that are not whitespace or '@'.@: A literal '@' symbol.[^\s@]+\.: The domain name, followed by a literal dot . (escaped with a backslash).[^\s@]+: The top-level domain.$: End of the string./^\(?(\d{3})\)?[-\s]?(\d{3})[-\s]?(\d{4})$/
This pattern matches formats like (123) 456-7890, 123-456-7890, and 123 456 7890.
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gi
A more complex example that finds http and https URLs.
* or +) is "greedy". Make it "lazy" by adding a ? after it (e.g., *?). This tells the engine to match the shortest possible string.\.. To match a backslash, use \\.(a*)*. Make your pattern more specific.