Concept
Custom Character Sets: [abc], [a-z], [^abc]
Square brackets build a custom character class. [abc] matches any of a, b, or c. [a-z] is a range. A leading ^ negates the set.
Basic set syntax
[abc] matches exactly one character: a, b, OR c. The order inside the brackets doesn't matter. Most regex metacharacters lose their special meaning inside a character class — you don't need to escape . or + inside [...].
Ranges
A hyphen between two characters creates a range based on Unicode code points. [a-z] matches any lowercase letter. [A-Fa-f0-9] matches any hex digit. [0-9a-zA-Z_] is equivalent to \w.
Negated sets
A caret at the start inverts the set. [^0-9] matches any single non-digit character. [^aeiouAEIOU] matches any consonant or non-letter. Important: the caret is only the negation operator when it's the FIRST character inside the brackets.
Escaping inside brackets
A hyphen is literal at the start or end of a set: [-abc] or [abc-]. A closing ] must be escaped (\]) or placed right after the opening [^. The backslash itself is \\. A caret is literal if it's not first: [a^] matches a or ^.
Related patterns
Hex Color Code
Match CSS hex color codes in both 3-digit (#RGB) and 6-digit (#RRGGBB) formats.
/#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})…/gHTML Tag Matcher
Match paired HTML tags and capture the tag name and inner content using a back-reference.
/<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>([\…/gDecimal Number
Matches decimal numbers, including integers and negatives.
/^-?\d+(\.\d+)?$/UUID (v1–v5)
Match RFC 4122 UUIDs (versions 1–5) in the standard 8-4-4-4-12 hex format.
/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a…/gi