Open In App

JavaScript RegExp (Regular Expression)

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It’s often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.).

In JavaScript, RegExp is an object that is used to create regular expressions, which are patterns used to match character combinations in strings.

JavaScript
// The /i flag makes the pattern case-insensitive
const patt = /Geeks/i; 
const s1 = "geeksforgeeks";
const s2 = "forgeeks";

console.log(patt.test(s1));
console.log(patt.test(s2));

Output
true
true

Creating a RegExp in JavaScript

There are two primary ways to create a RegExp in JavaScript

1. Using the RegExp Constructor

The RegExp constructor is useful when you need to create a regular expression dynamically, such as when the pattern or flags might change based on user input or other conditions.

JavaScript
let pattern = "hello";  // Pattern to match
let flags = "i";  // Case-insensitive flag
let regex = new RegExp(pattern, flags);

let s = "Hello world";

console.log(regex.test(s));

Output
true
  • Here, we dynamically create a RegExp object using the RegExp() constructor.
  • The pattern “hello” will match the string “Hello” because of the ‘i’ flag, which makes the search case-insensitive.
  • regex.test(s) returns true because “Hello” matches the pattern “hello” in a case-insensitive manner.

2. Using Regular Expression Literal

This is the most common and simple way to create a regular expression in JavaScript. It’s especially useful when the pattern is static (doesn’t change dynamically).

JavaScript
let regex = /hello/i;

let s = "Hello world";

console.log(regex.test(s));

Output
true
  • In this example, we define the regular expression directly using literal notation.
  • /hello/i means we’re looking for the word “hello”, and the ‘i’ flag ensures the search is case-insensitive.
  • regex.test(s) returns true because “Hello” matches the pattern “hello” case-insensitively.

Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-insensitive matching: 

ExpressionsDescriptions
gFind the character globally
iFind a character with case-insensitive matching
mFind multiline matching

Regular Expression Brackets can Find characters in a specified range

ExpressionsDescription
[abc]Find any of the characters inside the brackets
[^abc]Find any character, not inside the brackets
[0-9]Find any of the digits between the brackets 0 to 9
[^0-9]Find any digit not in between the brackets
(x | y)Find any of the alternatives between x or y separated with |

Regular Expression Metacharacters are characters with a special meaning:

MetacharacterDescription
\.Search single characters, except line terminator or newline.
\wFind the word character i.e. characters from a to z, A to Z, 0 to 9
\dFind a digit
\DSearch non-digit characters i.e all the characters except digits
\sFind a whitespace character
\SFind the non-whitespace characters.
\bFind a match at the beginning or at the end of a word
\BFind a match that is not present at the beginning or end of a word.
\0Find the NULL character.
\nFind the newline character.
\fFind the form feed character
\rFind the carriage return character
\tFind the tab character
\vFind the vertical tab character
\uxxxxFind the Unicode character specified by the hexadecimal number xxxxx

Regular Expression Quantifiers are used to define quantitiesoccurrence

QuantifierDescription
n+Match any string that contains at least one n
n*Match any string that contains zero or more occurrences of n
n?Match any string that contains zero or one occurrence of n
m{X}Find the match of any string that contains a sequence of m, X times
m{X, Y}Find the match of any string that contains a sequence of m, X to Y times
m{X,}Find the match of any string that contains a sequence of m, at least X times
m$Find the match of any string which contains m at the end of it
^mFind the match of any string which contains m at the beginning of it
?!mFind the match of any string which is not followed by a specific string m.

Regular Expression Object Properties:

PropertyDescription
constructorReturn the function that created the RegExp object’s prototype
globalSpecify whether the “g” modifier is set or not
ignorecaseSpecify whether the “i” modifier is set or not
lastindexSpecify the index at which to start the next match
multilineSpecify whether the “m” modifier is set or not
sourceReturn the text of RegExp pattern

Regular Expression Object Methods:

MethodDescription
compile()Used to compile the regular expression while executing of script
exec()Used to test for the match in a string.
test()Used to test for a match in a string
toString()Return the string value of the regular expression

Use Cases of Regular Expressions

1. Validating Email Addresses

Regular expressions are frequently used for validating emails. Here’s an example that matches most email formats

JavaScript
let regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
console.log(regex.test("[email protected]"));

Output
true

2. Extracting Specific Data from a String

You can use regular expressions to extract specific data from a string, like extracting all numbers from a text.

JavaScript
let regex = /\d+/g;
let res = "There are 123 apples and 456 oranges".match(regex);
console.log(res);

Output
[ '123', '456' ]

3. Replacing Substrings

Regular expressions are often used in replacing certain substrings in text.

JavaScript
let regex = /foo/g;
let res = "foo bar foo".replace(regex, "baz");
console.log(res);

Output
baz bar baz


Next Article

Similar Reads