Regex in JavaScript

Regex in JavaScript

Using Regex Methods in JavaScript

JavaScript provides several methods for working with regular expressions. These methods can be used with both the RegExp object and string literals.

RegExp Constructor

The RegExp constructor allows you to create a regular expression dynamically. It accepts the pattern as a string and optional flags.

javascript
	const pattern = 'hello';
	const flags = 'gi';
	const regex = new RegExp(pattern, flags);
	
	const str = 'Hello world, hello universe';
	console.log(str.match(regex)); // ['Hello', 'hello']

test Method

The test method checks if a pattern exists within a string. It returns true or false.

javascript
	const regex = /hello/i;
	console.log(regex.test('Hello world')); // true
	console.log(regex.test('Goodbye world')); // false

exec Method

The exec method searches for a match in a string and returns an array of matched results. If no match is found, it returns null.

javascript
	const regex = /(\d{3})-(\d{2})-(\d{4})/;
	const match = regex.exec('123-45-6789');
	
	if (match) {
	  console.log(match[0]); // '123-45-6789' (the entire match)
	  console.log(match[1]); // '123' (first capturing group)
	  console.log(match[2]); // '45' (second capturing group)
	  console.log(match[3]); // '6789' (third capturing group)
	}

String Methods with Regex

  1. match Method
    • The match method searches for a match in a string and returns an array of results or null.
javascript
	const str = 'The rain in Spain stays mainly in the plain';
	const regex = /ain/g;
	const matches = str.match(regex);
	console.log(matches); // ['ain', 'ain', 'ain']
  1. replace Method
    • The replace method searches for a match in a string and replaces it with a replacement string.
javascript
	const str = 'Hello world';
	const regex = /world/;
	const newStr = str.replace(regex, 'universe');
	console.log(newStr); // 'Hello universe'
  1. search Method
    • The search method searches for a match in a string and returns the index of the match or -1.
javascript
	const str = 'The quick brown fox';
	const regex = /quick/;
	const index = str.search(regex);
	console.log(index); // 4
  1. split Method
    • The split method splits a string into an array of substrings based on a regular expression.
javascript
	const str = 'apple, banana, cherry';
	const regex = /,\s*/;
	const fruits = str.split(regex);
	console.log(fruits); // ['apple', 'banana', 'cherry']

Example: Using Multiple Methods

javascript
	const str = 'The quick brown fox jumps over the lazy dog. The dog barked.';
	const regex = /the/gi;
	
	// Using match
	const matches = str.match(regex);
	console.log(matches); // ['The', 'the', 'The']
	
	// Using replace
	const newStr = str.replace(regex, 'a');
	console.log(newStr); // 'a quick brown fox jumps over a lazy dog. a dog barked.'
	
	// Using search
	const index = str.search(/fox/);
	console.log(index); // 16
	
	// Using split
	const words = str.split(/\s+/);
	console.log(words); // ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.', 'The', 'dog', 'barked.']

Exercises

  1. Write a regular expression to find all the words in a string and use the match method to return them as an array.
  2. Create a Regex pattern to replace all occurrences of the word “cat” with “dog” in a given string.
  3. Write a regular expression to split a string into sentences.

By leveraging these Regex methods in JavaScript, you can perform powerful string manipulations and pattern matching.