Functions & LogicSunday, January 25, 2026

Regular Expressions

Pattern matching and text validation.

Advertisement

Support JS Mastery Path by checking out our sponsors.

Regular Expressions (Regex) are cryptic but powerful. They allow you to match complex patterns in text.

Testing Patterns

const hasNumber = /d+/;
console.log(hasNumber.test("Hello 123")); // true
console.log(hasNumber.test("Hello World")); // false

Extracting Data (Capture Groups)

Modern JS allows "Named Capture Groups", which makes Regex readable.

const dateStr = "2026-01-30";
const pattern = /(?<year>d{4})-(?<month>d{2})-(?<day>d{2})/;

const match = pattern.exec(dateStr);
console.log(match.groups.year);  // "2026"
console.log(match.groups.month); // "01"
Advertisement

Support JS Mastery Path by checking out our sponsors.