You will be able to write JavaScript regular expression patterns that match specific text shapes, such as capitalized words, using anchors, character classes, and quantifiers.
What it is
A RegExp pattern is a compact description of text to match. In JavaScript, patterns are written between slashes or passed to new RegExp(). The pattern is not a normal string; it uses special syntax to describe sets of characters and repetitions. Related terms include pattern, flags, character class, quantifier, anchor, and match.
Mental model: read the pattern left to right as a set of rules. ^ says start, $ says end, [A-Z] says one uppercase letter, and [a-z]+ says one or more lowercase letters.
Why it matters
- Validate user input, such as names, codes, or identifiers.
- Find and replace text patterns without writing many loops.
- Parse simple structured strings, like CSV fields or log lines.
- Express rules that are hard to state with
includes()orindexOf(). - Build reusable matching logic with flags like
iandg.
Syntax or steps
Start with the smallest useful pattern: anchors plus character classes plus quantifiers.
- Choose a literal or class:
a,[A-Z],\\d. - Add a quantifier if needed:
+one or more,*zero or more,?zero or one,{n}exact count. - Add anchors to control position:
^start,$end. - Add flags outside the slashes:
icase-insensitive,gglobal,mmultiline.
Example
const re = /^[A-Z][a-z]+$/;
const tests = ["Alice", "alice", "A", "Alice1", "Alice Smith"];
for (const value of tests) {
console.log(value, re.test(value));
}
Expected output:
Alice true
alice false
A false
Alice1 false
Alice Smith false
Part by part:
/and/delimit the pattern.^requires the match to begin at the start of the string.[A-Z]matches exactly one uppercase ASCII letter.[a-z]+matches one or more lowercase ASCII letters.$requires the match to end at the end of the string.test()returnstrueif the whole string fits the pattern.
Common mistakes
- Forgetting anchors.
/[A-Z][a-z]+/matches inside"xAlice1"; use^and$for whole-string validation. - Using
+when*is intended.+requires at least one character;*allows zero. - Escaping incorrectly in
new RegExp(). In a string,"\\d"becomes the regex\d. - Assuming
gchangestest()behavior safely. Withg,test()advanceslastIndex, which can cause surprising repeated results.
When to use it
Use RegExp patterns when the rule is about shape. Use plain string methods when the rule is about exact content.
| Task | Use RegExp when | Use string methods when |
|---|---|---|
| Check a name | It must be capitalized letters only. | It must equal a known value. |
| Find digits | Any sequence of digits matters. | You only need one fixed substring. |
| Replace text | Multiple similar shapes need replacement. | The target is a literal phrase. |
Practice
Guided exercise: write a pattern that matches a lowercase word followed by a space and a lowercase word, such as "john doe".
const twoLowerWords = /^[a-z]+ [a-z]+$/;
console.log(twoLowerWords.test("john doe")); // true
console.log(twoLowerWords.test("John doe")); // false
Challenge: extend the pattern to allow an optional middle initial, such as "john a doe" or "john doe". Hint: use (?: [a-z])? for an optional non-capturing group. A possible pattern is /^[a-z]+(?: [a-z])? [a-z]+$/.
Quick check
Question: What does + mean in /^[A-Z][a-z]+$/?
Answer: It means the preceding character class [a-z] must appear one or more times.
Summary
RegExp patterns describe text shapes using classes, quantifiers, anchors, and flags. They are most useful when validation or matching depends on structure rather than exact equality. Start with small patterns, test them against real strings, and add complexity only when needed.