Negated character class
[^...] matches one character that is not in the set: [^0-9] is any non-digit, [^aeiou] any character except a lowercase vowel. The ^ negates only when it is the first character inside the brackets. Negated classes match newlines too, which makes [^>]* a reliable way to read up to a delimiter.
Syntax
[^set]Examples
"a1b2c3".replace(/[^0-9]/g, "")Result: "123"
Remove every character that is not a digit.
"sky myth dry hello".match(/\b[^aeiou\s]+\b/g)Result: ["sky", "myth", "dry"]
Words with no lowercase vowel; \s is excluded so the match stays inside one word.
'key="value" x'.match(/"([^"]*)"/)[1]Result: "value"
"a=1;b=2;c=3".match(/[^;]+/g)Result: ["a=1", "b=2", "c=3"]
Common mistakes
- Placing the caret later, as in [a^b], where it is a literal caret.
- Expecting [^x] at the end of the string to succeed: it still needs one character to consume; use (?!x) for a zero-width check.
- Using [^abc] to mean "not the word abc"; it excludes single characters. Use a negative lookahead for words.
- Forgetting that a negated class also matches newlines, which can let a match run onto the next line; add \n to the set when that matters.
Practice Negated character class
These drills check your answer on live data, free, no account needed.
- Negated class: no vowelsThe whole line must match. Match lines that contain none of the lowercase vowels a, e, i, o or u. Any other characters are fine, including uppercase letters, digits and spaces.
- Matching quote marksThe whole line must match. Match text wrapped in quotes, where the closing quote is the same kind as the opening one: both double or both single.
- key=value pairsLines hold key=value pairs separated by semicolons. Capture every pair: the key (word characters) in group 1 and the value (everything up to the next semicolon, possibly empty) in group 2.
Questions
How do I match everything except a specific character?
[^x]+ matches a run of characters that are not x. For a set, list them all: [^,;]+ stops at a comma or semicolon.
How do I match everything except a word?
A class cannot do it; use a negative lookahead: ^(?!.*\bword\b).*$ matches lines that do not contain the word.
Does [^x] match a newline?
Yes, which is why [^"]* is handy for quoted strings that span lines. Write [^"\n]* to stop at a line break.
Related
- Character classesWhat a character class matches, how ranges and negation work, what \d \w \s and their uppercase opposites mean, and what the dot does and does not match.
- Lookahead (?=...) and (?!...)How lookahead checks what follows without consuming it: numbers before a unit, words without a prefix, thousands separators and file name filters.
- Lazy vs greedyHow greedy quantifiers overshoot, how adding ? makes them lazy, why a negated class is often better than .*?, and the performance and newline traps.
- Regex for HTML tagsHow to remove HTML tags with <[^>]+>, list tag names, read an href value, match the content of one tag lazily, and why regex is not an HTML parser.