Back to JavaScript Notes
Topic #340

RegExp Introduction

You will be able to write a simple JavaScript regular expression, run it against a string, and explain what the pattern and flags do.

What it is

A RegExp object in JavaScript describes a pattern for matching text. Think of it as a tiny search engine for strings: you give it a pattern, and it can test whether text contains a match, find matches, or replace them.

Related terms include pattern, the rules you write; flags, options such as global or case-insensitive; match, a piece of text that fits the pattern; and capture group, a part of the pattern saved for later use.

Why it matters

  • Extracting data from text, such as numbers, dates, or IDs.
  • Validating simple input formats, such as phone numbers or ZIP codes.
  • Replacing or cleaning text, such as masking digits or normalizing whitespace.
  • Splitting strings on complex boundaries, such as multiple spaces or punctuation.
  • Searching case-insensitively without manually lowercasing everything.

Syntax or steps

  1. Create a regex literal: /pattern/flags.
  2. Or create one from a string: new RegExp("pattern", "flags").
  3. Use common flags: g for global, i for case-insensitive, m for multiline.
  4. Run it with string methods such as test, match, replace, or split.

Example

const text = "Order 123 shipped on 2024-05-01.";
const re = /\d+/g;

const matches = text.match(re);
console.log(matches); // ["123", "2024", "05", "01"]

const hasNumber = /\d/.test(text);
console.log(hasNumber); // true

const masked = text.replace(/\d+/g, "####");
console.log(masked); // "Order #### shipped on ####-##-##."

Part-by-part explanation

\d means “any digit.” The + means “one or more of the previous thing.” So \d+ matches a run of digits. The g flag tells match and replace to find every match, not just the first one.

text.match(re) returns an array of all digit runs. /\d/.test(text) returns true if at least one digit exists. text.replace(/\d+/g, "####") replaces each digit run with ####.

Common mistakes

  • Forgetting the g flag when you need all matches. Without g, match may return only the first match.
  • Reusing a global regex with test or exec without resetting lastIndex. The regex can remember where it stopped.
  • Using special characters like ., *, +, or ? as literal text without escaping them. Use \. to match a period.
  • Trying to validate complex formats, such as email addresses, with a tiny regex. Keep validation simple or use a tested library.

When to use it

Use a regex when the rule is pattern-based. Use plain string methods when the rule is simple and exact.

TaskUse regex whenUse string methods when
Find textYou need flexible patterns, such as digits or word boundaries.You need an exact substring, such as includes("Order").
Replace textYou need to replace all matching patterns, such as digits.You need to replace one known phrase.
Validate inputThe format has clear rules, such as ###-###-####.The check is simple, such as non-empty or starts with a letter.

Practice

Guided exercise: Write a regex that finds all words in "Buy 2 tickets for 3 people". Use /\b\w+\b/g with match.

Challenge: Replace every digit in "ID: 98765" with "X".

Expected output: "ID: XXXXX". Hint: use replace(/\d/g, "X").

Quick check

Question: What does /\d+/g match?

Answer: It matches one or more consecutive digits, and the g flag makes it find all such runs in the string.

Summary

A JavaScript regular expression is a pattern object that can test, find, and replace text. Start with small patterns, add flags deliberately, and use string methods when exact matching is enough.

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

Quick answers about learning RegExp Introduction in JavaScript.

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