Quantifiers
Quantifiers specify how many times a pattern must match. They apply to the preceding character, group, or class.
Quantifiers specify how many times a pattern must match. They apply to the preceding character, group, or class.
/a*/ // 0 or more "a"
/a+/ // 1 or more "a"
/a?/ // 0 or 1 "a" (optional)
/a{3}/ // exactly 3 "a"
/a{2,4}/ // 2 to 4 "a"
/a{2,}/ // 2 or more "a"
// Greedy vs lazy
/".+"/ // Greedy: matches as much as possible
/".+?"/ // Lazy: matches as little as possible
"say \"hi\" and \"bye\"".match(/".+"/g); // ["\"hi\" and \"bye\""] greedy
"say \"hi\" and \"bye\"".match(/".+?"/g); // ["\"hi\"", "\"bye\""] lazy
Add ? after a quantifier (+?, *?, {n,m}?) to make it lazy — it stops at the first valid match instead of the last.
More in JavaScript