RX

Character Classes

Regular Expressions · 8 entries

Basic Character Class [abc]

syntax
[abc]  matches a single a, b, or c
example
JS:  'gray vs grey'.match(/gr[ae]y/g)
Py:  re.findall(r'gr[ae]y', 'gray vs grey')
output
["gray", "grey"]

Note A character class matches exactly one character from the set. Most metacharacters lose their special meaning inside brackets -- for example, [.+*] matches a literal dot, plus, or asterisk.

Ranges [a-z] [A-Z] [0-9]

syntax
[a-z]  [A-Z]  [0-9]  [a-zA-Z0-9]
example
JS:  'Room 4B'.match(/[A-Z][0-9]/)
Py:  re.search(r'[0-9][A-Z]', 'Room 4B')
output
JS: null (no uppercase letter followed by digit)
Py: "4B" (digit followed by uppercase letter)

Note Order matters. [a-z] covers lowercase Latin letters. Combine ranges for broader matches. Ranges use Unicode/ASCII code points, so [A-z] accidentally includes [, \, ], ^, _, ` -- always use [A-Za-z] instead.

Negated Class [^abc]

syntax
[^abc]  matches any character EXCEPT a, b, or c
example
JS:  'a1b2c3'.match(/[^a-z]/g)
Py:  re.findall(r'[^a-z]', 'a1b2c3')
output
["1", "2", "3"]

Note The caret ^ must be the first character inside the brackets to negate. Placed anywhere else, it matches a literal ^. Common gotcha: [^abc] still matches newlines unless excluded.

\d Digit and \D Non-Digit

syntax
\d  =>  [0-9]    \D  =>  [^0-9]
example
JS:  'Invoice #2084'.match(/\d+/)
Py:  re.findall(r'\D+', 'abc-123-xyz')
output
JS: "2084"
Py: ["abc-", "-xyz"]

Note In Python 3 with the default engine, \d can match Unicode digits (e.g., Arabic-Indic digits) unless you use re.ASCII. In JavaScript, \d always means [0-9].

\w Word Character and \W

syntax
\w  =>  [a-zA-Z0-9_]    \W  =>  [^a-zA-Z0-9_]
example
JS:  'user_name@host'.match(/\w+/g)
Py:  re.findall(r'\W+', 'hello, world! 42')
output
JS: ["user_name", "host"]
Py: [", ", "! "]

Note \w includes the underscore. Like \d, Python's \w matches Unicode letters by default (accented chars, CJK, etc.) while JS \w sticks to ASCII unless the u flag is combined with Unicode property escapes.

\s Whitespace and \S

syntax
\s  =>  [ \t\n\r\f\v]    \S  =>  [^ \t\n\r\f\v]
example
JS:  'hello   world'.split(/\s+/)
Py:  re.split(r'\s+', '  line one\n  line two  ')
output
JS: ["hello", "world"]
Py: ["", "line", "one", "line", "two", ""]

Note \s matches spaces, tabs, newlines, carriage returns, form feeds, and vertical tabs. In Python, re.split on a leading/trailing whitespace produces empty strings at the edges -- use str.split() if you want to avoid them.

Dot Inside Character Class

syntax
[.]  matches a literal period (no escaping needed)
example
JS:  'v2.1.0'.match(/[.]/g)
Py:  re.findall(r'[.]', '192.168.0.1')
output
JS: [".", "."]
Py: [".", ".", "."]

Note Inside a character class, the dot loses its special meaning and matches a literal period. Both [.] and \. work for matching periods, but [.] can be more readable.

Custom Ranges & Combinations

syntax
[a-fA-F0-9]  [\w.-]  [\s\S]
example
JS:  '#ff8C2a'.match(/^#[a-fA-F0-9]{6}$/)
Py:  re.findall(r'[\w.-]+', 'file-name_v2.txt')
output
JS: ["#ff8C2a"]  (valid hex color)
Py: ["file-name_v2.txt"]

Note [\s\S] is a classic trick to match ANY character including newlines (before the s flag existed). Place a hyphen at the start or end of a class [-abc] or [abc-] to match it literally, or escape it [a\-z].