Script Valley
Regex: Actually Useful Patterns
Quantifiers and Greedy vs Lazy MatchingLesson 4.3

Negated character class vs lazy quantifier which to use

negated class as delimiter stopper, performance comparison, correctness differences, nested delimiter problem, practical choice guide

Two Ways to Stop at a Delimiter

Lazy vs negated class

Both ".*?" and "[^"]*" match a double-quoted string. They are not identical in performance or correctness.

// Lazy
'"hello world"'.match(/".*?"/g)   // ['"hello world"']

// Negated class — faster, more explicit
'"hello world"'.match(/"[^"]*"/g) // ['"hello world"']

Decision Rule

Use negated class when the delimiter is a single character. Use lazy when the ending condition is a multi-character sequence. For nested delimiters, use a parser.

Up next

Exact and range quantifiers in regex

Sign in to track progress