JavaScript RegExp

Regular expressions, often abbreviated as RegExp, provide a powerful way to search, match, and manipulate text patterns in JavaScript.

They allow for flexible and efficient string matching operations.

Let's explore the various aspects of regular expressions with code examples:

Creating Regular Expressions

Regular expressions in JavaScript are created using either the RegExpconstructor or the literal notation /pattern/.

// Using the RegExp constructor
const regex1 = new RegExp("pattern")

// Using the literal notation
const regex2 = "/pattern/"

In the above example, Both regex1and regex2represent regular expressions that match the pattern "pattern".

Basic Pattern Matching

Regular expressions can match specific patterns within strings using metacharacters and literal characters.


const str = "Hello, world!";

const regex = /world/;

console.log(regex.test(str)); // Output: true

In the above example, the regular expression /world/matches the substring "world"within the string "Hello, world!".

Example


const str = "Hello, world!";

const regex = /world/;

console.log(regex.test(str)); // Output: true
                

Plags

Regular expressions in JavaScript can have optional flags that modify their behavior. Common flags include i for case-insensitive matching and g for global matching.


const str = "Hello, world!";

const regex = /hello/i;

console.log(regex.test(str)); // Output: true

In the above example, the iflag, the regular expression /hello/imatches "Hello"regardless of case.

Example


const str = "Hello, world!";

const regex = /hello/i;

console.log(regex.test(str)); // Output: true
                

Character Classes

Character classes allow you to match any character from a specified set.


const str = "The quick brown fox jumps over the lazy dog";

const regex = /[aeiou]/g;

console.log(regex.test(str)); // Output: ["e", "u", "i", "o", "o", "u", "o", "e", "e", "a", "o"]

In the above example, the character class [aeiou]matches any vowel in the string.

Example


const str = "The quick brown fox jumps over the lazy dog";

const regex = /[aeiou]/g;

console.log(regex.test(str)); // Output: ["e", "u", "i", "o", "o", "u", "o", "e", "e", "a", "o"]
                

Quantifiers

Quantifiers specify the number of times a character or group of characters can occur.


const str = "aaaabbbbcccd";

const regex = /a{2,3}/g;

console.log(regex.test(str)); // Output: ["aaa"]

In the above example, The quantifier {2,3}in the regular expression /a{2,3}/matches "aa"in the string aaaabbbbcccd.

Example


const str = "aaaabbbbcccd";

const regex = /a{2,3}/g;

console.log(regex.test(str)); // Output: ["aaa"]

Substitution

Regular expressions can be used for search and replace operations using the replace()method.


const str = "Hello, world!";

const regex = /world/;

const newStr = (str.replace(regex, "universe"));

console.log(newStr); // Output: "Hello, universe!"

In the above example, the regular expression /world/is replaced with the string "universe"in the original string "Hello, world!".

Example


const str = "Hello, world!";

const regex = /world/;

const newStr = (str.replace(regex, "universe"));

console.log(newStr); // Output: "Hello, universe!"

By mastering regular expressions, you can perform complex string manipulations with ease.