theory.js

Regular Expressions (RegEx)

Basic Pattern Matching

Regular expressions (RegEx) help match patterns in strings, useful for searching and text manipulation.

  • RegEx patterns use special characters to define search criteria.
  • Common patterns: `\d` (digit), `\w` (word character), `.` (any character).
  • Flags modify behavior: `/pattern/g` (global), `/pattern/i` (case-insensitive).
const text = "The number is 42.";
const pattern = /\d+/g; // Matches one or more digits
console.log(text.match(pattern)); // ["42"]

const caseInsensitive = /hello/i;
console.log(caseInsensitive.test("HELLO")); // true