JavaScript RegExp – Overview

JavaScript RegExp – Overview

RegExp or Regular Expression is a pattern that is used to match or search for characters within a string. It is a powerful tool that can be used with JavaScript to validate inputs, extract information, or manipulate strings.

In JavaScript, RegExp is a built-in object that provides methods and properties to work with patterns. The regex can be written using the / symbol (e.g. /pattern/) or by invoking the RegExp constructor function (e.g. new RegExp("pattern")).

Syntax

RegExp follows a specific syntax to create a pattern. The pattern can be a simple string or a combination of characters and modifiers.

The basic syntax for creating a pattern using RegExp is:

/pattern/modifiers;
  • Pattern – a string that defines the expression to be searched.
  • Modifiers – optional parameters to change the search behavior. Some of the available modifiers are i (ignore case), g (global match), and m (multiline match).

For example:

var pattern = /hello/i;

This will create a RegExp object to search for the word “hello” in a case-insensitive manner.

Methods and Properties

Once a RegExp object is created, it can be used with several methods and properties to validate, search, or replace strings.

Some of the commonly used methods with RegExp are:

test()

The test() method checks if the pattern exists in a string and returns a boolean value.

Syntax:

RegExp.test(string);

Example:

var pattern = /hello/i;
var str = "Hello world!";
var result = pattern.test(str);

console.log(result); // true

exec()

The exec() method searches for the pattern in a string and returns an array containing the matched text, index, and groups.

Syntax:

RegExp.exec(string);

Example:

var pattern = /hello/i;
var str = "Hello world!";
var result = pattern.exec(str);

console.log(result); // ["Hello", index: 0, input: "Hello world!", groups: undefined]

match()

The match() method searches for the pattern in a string and returns an array containing the matched text and groups.

Syntax:

string.match(RegExp);

Example:

var pattern = /hello/i;
var str = "Hello world!";
var result = str.match(pattern);

console.log(result); // ["Hello"]

replace()

The replace() method replaces the pattern in a string with a new substring.

Syntax:

string.replace(RegExp, replaceWith);

Example:

var pattern = /hello/i;
var str = "Hello world!";
var result = str.replace(pattern, "Hi");

console.log(result); // "Hi world!"

search()

The search() method searches for the pattern in a string and returns the index of the first occurrence.

Syntax:

string.search(RegExp);

Example:

var pattern = /hello/i;
var str = "Hello world!";
var result = str.search(pattern);

console.log(result); // 0

Some of the commonly used properties with RegExp are:

source

The source property returns the text of the pattern.

Syntax:

RegExp.source;

Example:

var pattern = /hello/i;
console.log(pattern.source); // "hello"

lastIndex

The lastIndex property returns the index of the last string match.

Syntax:

RegExp.lastIndex;

Example:

var pattern = /hello/g;
var str = "Hello world! Hello from the other side!";
console.log(pattern.lastIndex); // 0

pattern.exec(str);
console.log(pattern.lastIndex); // 12

Flags

Flags are optional parameters that can be added to the regular expression to modify its behavior.

Some of the commonly used flags are:

i (ignore case)

The i flag makes the pattern case insensitive.

Example:

var pattern = /hello/i;
console.log(pattern.test("Hello World!")); // true

g (global)

The g flag makes the pattern search for all occurrences in the string.

Example:

var pattern = /hello/g;
console.log("Hello World! Hello from the other side!".match(pattern)); // ["Hello", "Hello"]

m (multiline)

The m flag makes the pattern search for matches in multiline strings.

Example:

var pattern = /^hello/m;
console.log("Hello\nWorld!".match(pattern)); // ["Hello"]

Conclusion

RegExp is a powerful pattern matching tool in JavaScript. It can be used to validate inputs, extract information, or manipulate strings. By using methods and properties with RegExp, developers can write more flexible code to handle pattern matching.

Like(0)