Regular Expressions (Regex) in JavaScript

Regular Expressions (Regex) are sequences of characters that form search patterns. They are used for pattern matching within strings and provide a powerful tool for text processing.

In this guide, we'll cover various regex concepts with examples, detailed explanations, and best practices.

Key Concepts

  • Literal Characters: Matches the exact character(s).
  • Metacharacters: Characters with special meaning, such as . for any character.
  • Character Classes: Define a set of characters to match.
  • Quantifiers: Specify how many times a character or group should be matched.
  • Anchors: Specify the position in the string to match, such as start ^ or end $.

1. Basic Matching

Basic matching involves checking if a string contains a specific sequence of characters.

regex.js
// Define the regex pattern to match the word "hello"
const pattern = /hello/;
const text = "Say hello to the world!";
console.log(pattern.test(text)); // Output: true
  • Pattern/hello/
  • Text"Say hello to the world!"
  • ResultThe pattern checks if "hello" exists in the text, returning true.

2. Case-Insensitive Matching

To match strings regardless of case, use the i flag.

regex.js
// Define the regex pattern to match "hello" case-insensitively
const pattern = /hello/i;
const text = "Say Hello to the world!";
console.log(pattern.test(text)); // Output: true
  • Pattern/hello/i
  • Text"Say hello to the world!"
  • ResultThe i flag makes the match case-insensitive, returning true even though "Hello" is capitalized.

3. Global Matching

To find all occurrences of a pattern in a string, use the g flag.

regex.js
// Define the regex pattern to match all instances of "hello"
const pattern = /hello/g;
const text = "Hello, hello, hello!";
console.log(text.match(pattern)); // Output: ["hello", "hello", "hello"]
  • Pattern/hello/g
  • Text"Hello, hello, hello!"
  • ResultThe g flag finds all matches in the text, returning an array of matches.

4. Matching Any Single Character

Use . to match any single character except newlines.

regex.js
// Define the regex pattern to match any characters between "h" and "o"
const pattern = /h.*o/;
const text = "Say h3ll0 to the world!";
console.log(pattern.test(text)); // Output: true
  • Pattern/h.*o/
  • Text"Say h3ll0 to the world!"
  • ResultThe .* matches any sequence of characters, so "h3ll0" is matched, returning true

5. Matching Any Character (Wildcard)

Use .* to match zero or more of any character.

regex.js
// Define the regex pattern to match any characters between "h" and "o"
const pattern = /h.*o/;
const text = "Say h3ll0 to the world!";
console.log(pattern.test(text)); // Output: true
  • Pattern/h.*o/
  • Text"Say h3ll0 to the world!"
  • ResultThe .* matches any sequence of characters, so "h3ll0" is matched, returning true.

6. Matching the Beginning of a String

Use ^ to match the beginning of a string.

regex.js
// Define the regex pattern to match "hello" at the beginning of the string
const pattern = /^hello/;
const text = "hello world!";
console.log(pattern.test(text)); // Output: true
  • Pattern/^hello/
  • Text"hello world!"
  • ResultThe ^ asserts that "hello" must be at the beginning, returning true.