The Plus Quantifier
You'll learn how to match one or more occurrences of a pattern using the + quantifier in regular expressions.
What Does + Mean?
The plus quantifier (+) tells the regex engine: "match the preceding character or pattern at least once, but keep going if there are more." It's like saying "one or more" in plain English.
Think of it as a greedy collector: if you're looking for digits with /\d+/, it won't stop at just one digit—it'll grab 5, or 42, or 99999 as a single match, as long as consecutive digits keep appearing.
How It Works
The + follows whatever you want to repeat:
const singleDigit = /\d/;
const oneOrMoreDigits = /\d+/;
singleDigit.test("7"); // true (matches "7")
oneOrMoreDigits.test("7"); // true (matches "7")
singleDigit.test("742"); // true (matches just "7")
oneOrMoreDigits.test("742"); // true (matches "742" as one match)
With character classes:
const pattern = /[a-z]+/;
pattern.test("hello"); // true (matches "hello")
pattern.test("a"); // true (matches "a")
pattern.test(""); // false (+ requires at least one)
pattern.test("123"); // false (no lowercase letters)
Plus vs No Quantifier
Without +, you match exactly one character. With +, you match one or more consecutive matches:
/go+d/.test("god"); // true (one 'o')
/go+d/.test("good"); // true (two 'o's)
/go+d/.test("goood"); // true (three 'o's)
/go+d/.test("gd"); // false (needs at least one 'o')
Key Takeaway: The + quantifier matches one or more consecutive occurrences of the preceding pattern—it requires at least one match but will greedily consume as many as possible.