JavaScript

Check Whether a String Matches a Regex in JS

For searching and pattern matching in JavaScript, regular expressions are used. When searching for a string or character within a sentence, the regex matches are taken into account. From a technical standpoint, we use a regular expression to extract the substring from a specified string.

This article will illustrate different methods to check whether the string matches a regex.

How to Check Whether a String Matches a Regex in JS?

To verify whether the string matches a regex in JavaScript or not, use the given-mentioned methods:

  • test() method
  • match() method
  • exec() method

Method 1: Check Whether a String Matches a Regex Using test() Method

To verify whether a string matches a regex, use the “test()” method. The test() method searches for a match among a regular expression/regex pattern and a string. It outputs “true” if the match exists/found else, it returns “false”.

Syntax
Use the given syntax to verify string matches the regex or not using the test() method:

pattern.test(string)

Example
Create two strings “string1” and “string2”:

var string1 = "https://linuxhint.com/";
var string2 = "Welcome To Linuxhint";

Create patterns of regular expression for strings:

var regexPattern1 = /^(https?):/;
var regexPattern2 = /i/g;

Here, “regexPattern1” will check whether the string contains “https?” in the specified string and the “regexPattern2” will determine whether the particular string contains “i” or not.

Call the “test()” method by passing strings to determine whether the strings match the pattern. If it gives “true”, it indicates that the string matches the pattern. If it gives “false”, it means the string does not match the regex pattern:

var result1 = regexPattern1.test(string1);
var result2 = regexPattern2.test(string2);

Print the result on the console using the “console.log()” method:

console.log(result1);
console.log(result2);

The given output displays “true” for both strings, which signifies that the strings match the respective regex patterns:

Method 2: Check Whether a String Matches a Regex Using match() Method

You can also use the “match()” method to verify whether a string matches a regex pattern or not. This method matches the string against the pattern and gives an array containing the matches. If the particular string does not match/satisfy with the given expression, it outputs null.

Syntax
Use the following syntax for the match() method:

string.match(pattern)

Example
Create a string:

var string = "Welcome To Linuxhint. It is a best Platform to Learn Skills";

Create a pattern that asks for subsets that contain the letter “e” followed by another letter:

var regexPattern = /e\w/g;

Invoke the match() method by passing the regex pattern as a parameter and store the resultant matches in a variable “result”:

var result = string.match(regexPattern);

Print the matches on the console:

console.log(result);

The output displays all the possible matches of the string with the pattern:

Method 3: Check Whether a String Matches a Regex Using exec() Method

You can also utilize the “exec()” method. The exec() method looks for matches in a string. If a match exists, this function returns the first match; otherwise, it returns null.

Syntax
If you want to get only the first match of the string, use the given syntax:

pattern.exec(string);

Example
In the given example, the string is searched according to the regex pattern with the help of the exec() method and returns the first match if it exists:

var result = regexPattern.exec(string);

As you can see, the output shows only the first match of the string:

We have provided all the essential instructions to verify whether a string matches a regex in JavaScript.

Conclusion

To check whether a string matches a regex, use the “test()” method, “match()” method, or the “exec()” method. test() method outputs “true” if the specified string matches a regex. match() method returns the array of matches of the string, while the exec() method gives only the first match. In this article, we illustrated different methods to check whether the string matches a regex or not.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.