Back to JavaScript Notes
Topic #348

RegExp Methods

You will be able to test strings against regular expressions and extract matches using JavaScript RegExp methods such as test and exec, plus String methods like match and matchAll.

What it is

A regular expression is a pattern object. In JavaScript, you can create one with a literal such as /\d{4}/ or with new RegExp("\\d{4}"). Think of a RegExp as a search tool: test asks “is there a match?”, while exec asks “what is the next match?”.

RegExp.prototype.test returns a boolean. RegExp.prototype.exec returns a match array or null. String methods such as match, matchAll, replace, search, and split accept RegExp objects and return strings, arrays, or iterators.

Related terms: pattern, flags (g, i, m, s, u, y), capture group, lastIndex, and match object.

Why it matters

  • Validate input quickly, such as dates, IDs, or email-like strings.
  • Extract structured data from logs, user text, or API responses.
  • Replace or normalize text without manual loops.
  • Search case-insensitively or across multiple lines.
  • Build simple parsers for formats like YYYY-MM.

Syntax or steps

  1. Create a pattern: const re = /\d{4}/;.
  2. Test it: re.test("2026-01") returns true.
  3. Match once: "2026-01".match(/\d{4}/) returns ["2026"].
  4. Match all: "2026-01".match(/\d+/g) returns ["2026", "01"].
  5. Use exec when you need groups and position: /(\d{4})-(\d{2})/.exec("2026-01").

Example

const text = "Invoice 2026-01 total 42.50 USD";

const hasYear = /\d{4}/.test(text);
const year = text.match(/\d{4}/);
const numbers = text.match(/\d+(?:\.\d+)?/g);
const dateParts = /(\d{4})-(\d{2})/.exec(text);

console.log(hasYear);
console.log(year);
console.log(numbers);
console.log(dateParts);

Part-by-part:

  • test checks whether the text contains four digits and returns true.
  • match without g returns the first match as an array, including index and input properties.
  • match with g returns all matched strings: ["2026", "01", "42.50"].
  • exec returns the full match and capture groups: ["2026-01", "2026", "01", ...].
  • The pattern \d+(?:\.\d+)? matches one or more digits, optionally followed by a dot and more digits.

Common mistakes

  • Forgetting g when using match for all matches. Without g, match returns only the first match.
  • Reusing a global or sticky RegExp with test or exec without resetting lastIndex. The next call may start after the previous match.
  • Assuming match always returns an array. It returns null when there is no match.
  • Using match with g and expecting capture groups. It returns only matched strings; use matchAll, which requires g, or an exec loop for groups.

When to use it

GoalUseWhy
Only yes or notestFast boolean result.
First match with groupsexec or match without gReturns groups, index, and input.
All simple matchesmatch with gReturns an array of matched strings.
All matches with groupsmatchAllReturns an iterator of full match arrays.

Use test for validation, match for simple extraction, exec when you need one detailed match, and matchAll when you need every match with groups.

Practice

Guided exercise: Given "user-42@example.com", extract the number.

const id = "user-42@example.com".match(/\d+/);
console.log(id[0]); // "42"

Challenge: Extract all years from "2024, 2025, 2026" and return an array. Hint: use the g flag.

const years = "2024, 2025, 2026".match(/\d{4}/g);
console.log(years); // ["2024", "2025", "2026"]

Quick check

Question: What does /\d{4}/.test("2026-01") return?

Answer: true, because the string contains four consecutive digits.

Summary

RegExp methods let you test and extract patterns from strings. Choose test for validation, match for simple extraction, and exec or matchAll when you need groups or multiple matches.

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 Methods – FAQs

Quick answers about learning RegExp Methods in JavaScript.

This free note from Coding Now Tech Institute explains RegExp Methods 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 Methods, is 100% free with no signup required.
With focused practice, most students grasp RegExp Methods 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