Level 2: Practical patterns · Lesson 13 of 30
Negated sets [^ ]
A caret at the start of a character set flips it: [^,] matches any single character except a comma. Negated sets are perfect for reading up to a delimiter.
You already know that [abc] matches one of the listed characters. Put a caret ^ immediately after the opening bracket and the meaning flips: [^abc] matches any single character that is not a, b or c. Digits, spaces, punctuation and other letters all qualify.
Negated sets shine when you want everything up to a delimiter. In a comma-separated line like red,green,blue, the pattern [^,]+ matches each field: one or more characters that are not commas. Compare that with .+, which grabs the entire line because the dot also accepts commas.
Quoted text is another classic. "[^"]*" matches an opening quote, any number of characters that are not quotes, and a closing quote. On He said "yes" and "no" it produces "yes" and "no" as two separate matches. The greedy version ".*" would produce a single match from the first quote to the last.
The negated set is usually faster and more predictable than the lazy alternative ".*?". Both work in JavaScript, Python, PHP, Java, C#, Ruby, Go and Rust, but the negated set cannot overreach past the delimiter and makes your intention obvious to the next reader.
Watch out for newlines. Unlike the dot, a negated set matches newline characters unless you exclude them. [^,]+ happily crosses line breaks. If you want to stay on one line, add the newline to the set: [^,\n]+. This difference catches many people the first time.
The caret only means not when it is the very first character inside the brackets. In [a^b] it is a literal caret. You can still use ranges and shorthands inside a negated set: [^0-9] matches any non-digit, which is the same as \D, and [^\s,] matches anything that is not whitespace or a comma.
Remember that a negated set still consumes exactly one character, just like a normal set. [^x] does not mean nothing here; it means one character that is not x. At the end of the text there is no character left, so [^x] cannot match there. If you need to assert absence without consuming, you will want lookarounds, covered later in the course.
Live tester
3 matches (JavaScript engine)
red,green,,blue
Examples
Numbers
/[^,]+/g10,20,30
Matches: “10”, “20”, “30”
Each comma-separated number is matched as its own field.
Text
/"[^"]*"/gsay "hi" and "bye"
Matches: “"hi"”, “"bye"”
Each quoted word is matched separately because the set refuses to cross a quote.
Codes
/[^-\s]+/gAB-12 CD-34
Matches: “AB”, “12”, “CD”, “34”
Splitting on hyphens and whitespace gives the parts AB, 12, CD and 34.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match each non-empty field in the comma-separated line.
red,green,,blue
Task 2: Match each quoted word including its quotes, as separate matches.
He said "yes" and "no" today
Task 3: Match each run of non-digit characters.
ab12cd345ef