Mastering Regular Expression Metacharacters: A Complete Guide
This article provides a comprehensive, English-language reference of common regular expression metacharacters, explaining their meanings, usage rules, and examples for pattern matching in programming languages such as JavaScript.
This article provides a comprehensive reference of common regular expression metacharacters and their usage. \: Marks the next character as a special character, text, backreference, or octal escape. For example, "n" matches the character "n"; "\n" matches a newline. ^: Matches the start of the input string. If the RegExp object's Multiline property is set, ^ also matches the position after "\n" or "\r". $: Matches the end of the input string. With Multiline enabled, $ also matches the position before "\n" or "\r". *: Matches the preceding character or subexpression zero or more times. +: Matches the preceding character or subexpression one or more times. ?: Matches the preceding character or subexpression zero or one time. {n}: n is a non‑negative integer; matches exactly n times. {n,}: n is a non‑negative integer; matches at least n times. {n,m}: n and m are non‑negative integers with n ≤ m ; matches between n and m times (inclusive). Note: no spaces are allowed between the numbers and the comma. ??: When this character follows another quantifier (*, +, ?, {n}, {n,}, {n,m}), it makes the quantifier non‑greedy, matching the shortest possible string. .: Matches any single character except "\n". Use a character class like "[\s\S]" to match any character including newlines. (pattern): Captures pattern as a subexpression; captured groups can be referenced with $0…$9. To match literal parentheses, escape them as "\(" or "\)". (?:pattern): Matches pattern without capturing it (non‑capturing group), useful for grouping with alternation. (?=pattern): Positive look‑ahead; asserts that pattern follows the current position without consuming characters. (?!pattern): Negative look‑ahead; asserts that pattern does not follow the current position. x|y: Matches either x or y . [xyz]: Character class; matches any one of the listed characters. [^xyz]: Negated character class; matches any character not listed. [a-z]: Character range; matches any character within the specified range. [^a-z]: Negated range; matches any character outside the specified range. \b: Word boundary; matches the position between a word character and a non‑word character. \B: Non‑word boundary; matches positions that are not word boundaries. \cX: Matches the control character indicated by X . \d: Digit character; equivalent to [0-9]. \D: Non‑digit character; equivalent to [^0-9]. \f: Form feed; equivalent to \x0c or \cL. \n: Newline; equivalent to \x0a or \cJ. \r: Carriage return; equivalent to \x0d or \cM. \s: Whitespace character (space, tab, form feed, etc.); equivalent to [ \f\n\r\t\v]. \S: Non‑whitespace character; equivalent to [^ \f\n\r\t\v]. \t: Tab character; equivalent to \x09 or \cI. \v: Vertical tab; equivalent to \x0b or \cK. \w: Word character (letters, digits, underscore); equivalent to [A-Za-z0-9_]. \W: Non‑word character; equivalent to [^A-Za-z0-9_]. \xNN: Hexadecimal escape where NN is a two‑digit hex code (e.g., \x41 matches "A"). \num: Matches a backreference num if that many capturing groups exist; otherwise, if num is an octal digit (0‑7), it is an octal escape. \n: Octal escape or backreference depending on preceding captures; if at least n groups exist, it is a backreference, otherwise an octal escape. \nm: Similar to \n, but with two digits; interpreted as a backreference if enough groups exist, otherwise as an octal value. \nml: Octal escape with three digits where n is 0‑3 and m , l are 0‑7. \uNNNN: Unicode escape; matches the character represented by the four‑digit hexadecimal number NNNN .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
