Regex operators reference
A practical reference to regex operators: quantifiers, anchors, classes, groups, lookarounds and flags. Covers how engine differences and Unicode affect pattern-based text changes.
Regular expressions describe patterns in text. Paired with a replace operation, they become a text transformation: matched characters are rewritten according to a template. This reference covers the operators most patterns rely on. It also covers what a regex replacement actually changes, and where patterns and their output stop behaving as expected once they move between tools, languages and readers.
Is a regex transformation the right tool?
Regex works well when the text you want to find or rewrite follows a describable shape. Examples include log lines, identifiers, delimited fields, repeated whitespace and simple reorderings such as swapping two captured parts. The main benefit is precision: one pattern can handle many variants that a literal search would miss. The decisive tradeoff is fragility. Patterns are terse and depend on a specific engine. They also tend to fail quietly on input their author did not anticipate, especially non-ASCII text. A pattern that is correct on a sample can still damage real data.
Plain literal find-and-replace, or a dedicated parser, is preferable in these cases:
- The target is a fixed string with no variation, so a literal search is clearer and needs no escaping.
- The input has nested or recursive structure, such as HTML, XML, JSON or source code, where a parser is more reliable.
- The text includes many languages, accents or emoji and the engine in use has no grapheme-aware matching.
- Other people must maintain the transformation and cannot easily read or verify the pattern.
- The input is untrusted and the engine is a backtracking engine with no time limit (see the performance note below).
Core regex operators
Most of the syntax below is shared by the widely used backtracking flavors, such as those in JavaScript, Python, Java, .NET and PCRE. Where support differs, the table says so. Always confirm against the documentation for the exact engine and version you will run.
Literals and escaping
Most characters match themselves. The metacharacters . ^ $ * + ? ( ) [ ] { } | and \ have special meanings. To match one of them literally, precede it with a backslash, so . matches a period and ( matches an opening parenthesis. Inside a character class the rules change. Most metacharacters lose their special meaning there, while ], , ^ (at the start) and - (between characters) still need care.
Quantifiers
| Operator | Meaning | Notes |
| • | Zero or more of the preceding item | Greedy: takes as much as possible, then backtracks |
| • | One or more | Greedy |
| ? | Zero or one | Also used as a modifier after other quantifiers |
| {n} | Exactly n | |
| {n,} | n or more | |
| {n,m} | Between n and m, inclusive | |
| *? +? ?? {n,m}? | Lazy versions | Take as little as possible; not available in POSIX ERE |
| *+ ++ ?+ | Possessive versions | Never give back characters; supported in PCRE, Java and Python 3.11+, not in JavaScript |
Anchors and boundaries
| Operator | Meaning | Notes |
| ^ | Start of input, or start of a line in multiline mode | |
| $ | End of input, or end of a line in multiline mode | In several engines it can also match before a final newline |
| \b | Word boundary | What counts as a word character depends on the engine (see \w) |
| \B | Not a word boundary | |
| \A | Absolute start of input | Not supported in JavaScript |
| \z | Absolute end of input | Python uses \Z for this; not supported in JavaScript |
Character classes
| Operator | Meaning | Notes |
| [abc] | Any one of a, b or c | |
| [^abc] | Any one character except a, b or c | |
| [a-z] | A range of characters | Ranges follow code point order, not alphabetical order in any language |
| . | Any single character except a line break | Dot-all mode lets it match line breaks; what counts as one character varies (see the Unicode section) |
| \d \D | Digit / non-digit | ASCII 0–9 only in JavaScript; Unicode digits in Python 3 string patterns by default |
| \w \W | Word character / non-word character | ASCII letters, digits and underscore in JavaScript; broader Unicode coverage in Python 3 string patterns |
| \s \S | Whitespace / non-whitespace | Which Unicode spaces are included varies by engine and mode |
| [[:digit:]] [[:alpha:]] | POSIX bracket classes | Used in POSIX tools; not supported in JavaScript or Python's re |
| \p{L} \p{Mn} | Unicode property classes | JavaScript (with the u or v flag), PCRE, Java and .NET support them; Python's built-in re does not |
Groups, alternation and backreferences
| Operator | Meaning | Notes |
| a|b | Alternation: match a or b | Has very low precedence, so group it to limit its scope |
| ( ... ) | Capturing group | Numbered from 1 by the position of the opening parenthesis |
| (?: ... ) | Non-capturing group | Groups without storing a capture |
| (?<name> ... ) | Named capturing group | Syntax in JavaScript, PCRE, Java and .NET; Python uses (?P<name> ... ) |
| \1 | Backreference to group 1 | Matches the same text that group 1 captured |
| \k<name> | Backreference to a named group | Python uses (?P=name) |
| (?> ... ) | Atomic group | Discards backtracking positions inside the group; PCRE, Java, .NET and Python 3.11+, not JavaScript |
Lookarounds
| Operator | Meaning | Notes |
| (?= ... ) | Positive lookahead: followed by | Consumes no characters |
| (?! ... ) | Negative lookahead: not followed by | |
| (?<= ... ) | Positive lookbehind: preceded by | Python's re requires fixed width; JavaScript allows variable length; other engines differ |
| (?<! ... ) | Negative lookbehind: not preceded by | Same width rules as positive lookbehind |
Flags and modes
| Flag | Effect | Notes |
| i | Case-insensitive matching | Case folding for non-ASCII letters varies by engine |
| m | ^ and $ match at line boundaries | |
| s | Dot matches line breaks | Python calls this DOTALL |
| x | Free-spacing mode, allowing whitespace and comments in the pattern | Not available in JavaScript |
| g | Find all matches rather than the first | JavaScript-specific; other languages use separate functions for this |
| u / v | Unicode-aware matching in JavaScript | Required for \p{...}; makes the dot match a full code point |
Replacement syntax
The replacement template is not regex syntax, and it differs by language. JavaScript uses $1 and $<name>. Python uses \1, \g<1> and \g<name>. .NET uses $1 and ${name}. A template copied from one language may insert literal text instead of the captured value in another.
Pattern: ^(\w+),\s*(\w+)$
Replacement: $2 $1 (JavaScript)
\2 \1 (Python)
Input: Lovelace, Ada
Output: Ada Lovelace
In this example the first group captures "Lovelace", the comma and optional whitespace are matched but not captured, and the second group captures "Ada". The same pattern fails on "Brontë, Anne" in JavaScript because \w there is ASCII-only. In Python 3 the precomposed ë matches \w. If the ë is stored as an e followed by a combining diaeresis, however, the result depends on how the engine classifies combining marks. Normalizing the input first removes that uncertainty.
What a regex replacement actually changes
Matching alone changes nothing: it reports positions and captured text. A replacement produces new text in which the stored characters are different. This is unlike CSS transforms, font choices or bidirectional display rules, which alter how unchanged characters look. Anything that later reads the output, whether a search index, a screen reader or another program, sees the rewritten characters.
Some terms from the wider text-tools world mean something different, or nothing, in regex. Lookbehind looks backward from the current position, but it does not reverse text. A regular expression cannot reverse a string; that needs code that splits the text into units and reorders them. "Mirror" and "font" are not regex concepts. Font-style generators substitute distinct Unicode characters, such as mathematical bold letters, for ordinary ones. A class such as [A-Za-z] does not match those substitutes, so a pattern written for ordinary text will skip styled text entirely.
What counts as one character
The dot and each quantified item consume one unit, and the size of that unit depends on the engine and its flags. It can be a UTF-16 code unit (JavaScript without the u flag), a code point (JavaScript with u, Python 3 strings) or a grapheme cluster (\X in PCRE and Perl, or in Python's third-party regex module). What a reader perceives as one character can span several code points.
Input: é written as U+0065 U+0301 (e + combining acute accent)
^.$ no match in code-point mode: the input is two code points
^\X$ matches where \X is supported: one grapheme cluster
Input: 👍🏽 written as U+1F44D U+1F3FD (thumbs up + skin tone modifier)
JavaScript without u: four UTF-16 code units, so ^.$ does not match
JavaScript with u: two code points, so ^.$ still does not match
Grapheme-aware \X: one cluster, so ^\X$ matches
This matters for transformations. A replacement that deletes characters outside [A-Za-z0-9] removes combining accents and changes words. A replacement that operates on single code units can split an emoji, leaving a stray skin-tone modifier or an unpaired surrogate that displays as a replacement box. If your input may contain such text, normalize it first, typically to NFC. Then use Unicode-aware flags, and use grapheme matching where the engine offers it.
Will patterns and results survive copying and sharing?
Two different things get copied: the pattern itself and the text a replacement produces. They fail in different ways.
Moving a pattern between environments
A pattern copied from a tester, a forum answer or another codebase may rely on features its new engine lacks, such as lookbehind, possessive quantifiers, \A, named-group syntax or property classes. Depending on the engine and the feature, the result can be a syntax error or a pattern that compiles but matches differently. The string-literal context also changes how many backslashes the pattern needs:
Regex as written: \d+\.\d+
Python raw string: r"\d+\.\d+"
JavaScript literal: /\d+\.\d+/
JavaScript string: new RegExp("\\d+\\.\\d+")
JSON string value: "\\d+\\.\\d+"
Copying from formatted documents or web pages can also introduce characters that look correct but are not. Smart quotes, non-breaking spaces and zero-width characters all change what a pattern matches without any visible difference. If a pattern that looks correct fails, inspect it for such characters before rewriting the logic.
Using the transformed text
Regex output is ordinary stored text, so it copies and pastes as whatever characters the replacement produced. The usability risks come from what the pattern removed or inserted:
- Reading order: in mixed right-to-left and left-to-right text, deleting or inserting bidirectional control characters (such as U+200F, the right-to-left mark) changes the displayed order of words without changing the visible characters themselves.
- Search and editing: if a replacement swaps ordinary letters for look-alike or styled Unicode characters, plain searches for the original words no longer find them, and spell checkers may flag them.
- Assistive technology: screen readers read the stored characters, so ordinary letters left intact remain readable. Styled or look-alike substitutes are different code points and may be announced differently or skipped. No scoped test was performed for this article, so check the specific screen reader and platform your audience uses.
- Downstream parsing: output fed into filenames, identifiers, CSV fields or code must avoid characters those formats treat specially, which may require a second escaping step.
Produce or keep a plain-text equivalent when the output will be indexed, read aloud, used as an identifier or edited by others. A plain-text equivalent is ordinary characters in logical order, without styling substitutes or stray control characters.
Testing patterns before relying on them
An online or editor-based regex tester shows results for the one flavor it implements. A pattern that works there is only confirmed for that flavor, so select the flavor that matches your runtime, or retest in the runtime itself. Useful edge cases include empty input, multiline input, trailing newlines, accented and decomposed characters, emoji and right-to-left text. Do not assume a web-based tester keeps input on your machine. Unless its documented policy says otherwise, test with synthetic data rather than real records or credentials.
Performance on untrusted input
Backtracking engines can take exponential time on certain patterns. Nested quantifiers over overlapping content are the classic case:
Pattern: ^(a+)+$
Input: aaaaaaaaaaaaaaaaaaaaaaaaaaaaab
The trailing b forces the engine to try every way of splitting the
run of a's between the inner and outer quantifier before failing.
Mitigations include rewriting the pattern to remove ambiguity (here, ^a+$), using atomic groups or possessive quantifiers where supported, applying input-length or time limits, or using a linear-time engine such as RE2. Linear-time engines omit backreferences and lookarounds, so some patterns must be restructured.
Evidence and gaps
No subject-specific evidence was supplied for this reference: no particular tool, tester or engine version was evaluated, and no tests were run. The operator meanings and engine differences above reflect general documented behavior of common regex flavors, and support for individual features changes between versions. Verify any operator you depend on against the documentation for the engine and version in your environment, and test with representative input, including non-ASCII text, before applying a replacement to real data.