Negated classes
[^...] matches any one character except those listed. Why [^"]* is the workhorse for quoted text, why it crosses newlines, and why it needs a character.
Negated classes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- [^abc] matches any single character that is not a, b or c, including spaces, punctuation and newlines.
- A negated class still consumes exactly one character. /a[^b]/ does not match the string 'a', because there is nothing after the a.
- [^"]* means 'any run of characters up to the next quote'. It is the standard way to capture a quoted value, and it stops at the first closing quote.
- Unlike the dot, a negated class matches a line break, so [^"]* can run across lines. Exclude \n explicitly if you do not want that.
- The caret only negates when it is first. In [a^] it is a literal caret, and [^^] means anything except a caret.
Describing what you do not want
You have a line of key-value text and you want the values inside the quotes:
name="Ada Lovelace" role="admin" id="7"
A value can contain any character: letters, spaces, digits. Listing every allowed character is hopeless. But the one thing a value cannot contain is the closing quote. So describe that: any character except ". A negated class does it. Put ^ right after the opening bracket and the class flips from “one of these” to “one of anything but these”.
const line = `name="Ada Lovelace" role="admin" id="7"`;
line.match(/"[^"]*"/g)
// [ '"Ada Lovelace"', '"admin"', '"7"' ]
Read "[^"]*" left to right: a quote, then zero or more characters that are not a quote, then a quote. Since the middle can never contain a quote, it ends exactly at the next one. That makes [^"]* one of the most useful fragments in regex. The * here means “any number of times” and is the topic of the next group of lessons. Putting parentheses around the pieces gives key and value in one pass:
[...line.matchAll(/(\w+)="([^"]*)"/g)].map(m => m[1] + " -> " + m[2])
// [ 'name -> Ada Lovelace', 'role -> admin', 'id -> 7' ]
Compare the tempting alternative, the dot:
line.match(/".*"/g)
// [ '"Ada Lovelace" role="admin" id="7"' ]
.* ran from the first quote to the last one on the line and swallowed everything. The greedy and lazy lesson explains why, and shows that the negated class is usually better than the fix.
Other everyday negations
Remove everything that is not a digit:
"a1b22c".match(/[^0-9]+/g) // [ 'a', 'b', 'c' ]
"a1b22c".replace(/[^0-9]/g, "") // 122
The ranges from the character classes lesson work inside a negated class as well. "Ab Cd".match(/[^a-z]/g) gave ['A', ' ', 'C']: uppercase letters and the space are all “not a lowercase letter”. Be careful about what you are negating. [^a-z] is not “an uppercase letter”, it is everything else, including punctuation, digits and spaces.
A negated class still needs a character
[^b] means “one character that is not b”. It does not mean “not b here”. So it fails if there is no character at all:
/a[^b]/.test("a") // false
/a[^b]?/.test("a") // true
/a[^b]*/.exec("a")[0] // "a"
At the end of the string the class has nothing to consume. If you mean “a followed by anything but b, or by nothing”, you need the optional version, or a lookahead (a later lesson).
Negated classes match newlines
The dot is the other “any character”. By default it refuses line breaks, but a negated class refuses only what you list:
/a.b/.test("a\nb") // false
/a[^x]b/.test("a\nb") // true
/[^a]/.test("\n") // true
`"first\nsecond" tail`.match(/"[^"]*"/)[0].split("\n").length // 2
The last line is a quoted value that spans two lines. [^"]* crossed the newline and matched it. Often that is exactly what you want, such as a multi-line string. If it isn’t, add the line break to the exclusion: [^"\n]*. The next lessons on the dot and on anchors go deeper on line handling.
The caret only counts first
The ^ is special only as the first character after [. Anywhere else it is a plain caret:
"a^b".match(/[a^]/g) // [ 'a', '^' ]
"a^b".match(/[^^]/g) // [ 'a', 'b' ]
And in JavaScript, [^] is a class that excludes nothing, so it matches any character, newline included. An unfinished class such as [^abc fails with Unterminated character class.
In grep and Python
The idea carries over unchanged. printf 'say "hi" and "bye"\n' | /usr/bin/grep -o '"[^"]*"' printed "hi" and "bye". In Python, re.findall(r'"[^"]*"', 'say "hi" and "bye"') gave the same two. Python agreed that a negated class matches a newline (re.search(r"[^a]", "\n") is truthy) while the dot does not (re.search(r".", "\n") returned None). A two-line test with perl -0777 matched as well.
About the video
The embed is a clip of Codeimas’s “Complete Regex Course for Beginners (2 Hours)”, published on 2 August 2026 and 1 hour 54 minutes 52 seconds long. It plays the chapter “Negated Classes & Shorthands”, which runs from 12:22 to 28:28 in the description’s chapter list. The chapter title says it covers this lesson and the next one, so the next lesson links the same chapter under “deeper”. The description says the course teaches regex through JavaScript coding challenges. I couldn’t read a transcript, so I can’t say how it handles the newline behaviour or [^"]*, and the channel has a small audience, so I can’t point to outside reviews of its accuracy. English per its title and description.
What I did not run
GNU grep and regex101. I did not test any other flavour’s handling of [^], which is a JavaScript oddity.
Next
Characters like digits, word characters and spaces come up so often that regex gives them one-letter names: Shorthand classes.
Check yourself
What does "a1b22c".replace(/[^0-9]/g, "") return?
"122". The class matches every character that is not a digit, and the replacement deletes each one.
Does /a[^b]/ match the string 'a'?
No. The class must consume one character that is not b, and the string ends after the a. Making it optional with /a[^b]?/ would match.
On name="Ada Lovelace" role="admin" what does "[^"]*" with the g flag find?
"Ada Lovelace" and "admin" as two separate matches. Each run stops at the next quote, unlike ".*", which would run to the last quote on the line.
Why can [^"]* span two lines when .* cannot?
The dot excludes line terminators by default, but a negated class excludes only the characters you list. A newline is not in the list, so it matches.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.