Back to JavaScript Notes
Topic #342

RegExp Patterns

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() or indexOf().
  • Build reusable matching logic with flags like i and g.

Syntax or steps

Start with the smallest useful pattern: anchors plus character classes plus quantifiers.

  1. Choose a literal or class: a, [A-Z], \\d.
  2. Add a quantifier if needed: + one or more, * zero or more, ? zero or one, {n} exact count.
  3. Add anchors to control position: ^ start, $ end.
  4. Add flags outside the slashes: i case-insensitive, g global, m multiline.

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() returns true if 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 g changes test() behavior safely. With g, test() advances lastIndex, 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.

TaskUse RegExp whenUse string methods when
Check a nameIt must be capitalized letters only.It must equal a known value.
Find digitsAny sequence of digits matters.You only need one fixed substring.
Replace textMultiple 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

RegExp Patterns – FAQs

Quick answers about learning RegExp Patterns in JavaScript.

This free note from Coding Now Tech Institute explains RegExp Patterns in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including RegExp Patterns, is 100% free with no signup required.
With focused practice, most students grasp RegExp Patterns in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now