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.
character setmatch one ofcharacter classbracket expressionalternative characters
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.
letter rangedigit rangealphanumericcharacter range
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.
negated classnot matchingexclude characterscaret in brackets
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].
match digitsextract numbersnon-digit\d shorthandnumeric match
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.
word characteralphanumericunderscorenon-word character\w shorthand
\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 ')
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.
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.
literal dotdot in bracketsmatch periodcharacter class dot
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].
hex digitscustom rangecombine classesmatch everythinghyphen in class