JavaScript

JavaScript | RegExp test() Method

The RegExp test() method is used to match a pattern detonated by the RegExp with a string and return true or false depending upon whether the match is found or not. The test() method is a method of the RegExp Object in the JavaScript programming language. This article will explain the RegExp test() method along with examples, but first, let’s start with the syntax:

Syntax of the test() Method

result = regExpVar.test(matchString)

In this syntax:

  • result is the variable in which the return value of the test() method will be stored
  • regExpVar is the variable that contains the regular expression to match
  • matchString is the string that will be matched against the regular expressions

Return Value

The return value of this method is of the Data Type boolean, returns true if the match is found else false.

Example 1: Trying the Find the Character “h” Inside a String

Start by creating a string variable and give it some string value with the following line:

stringVar = "Hello World, This is LinuxHint";

The next step is to actually create the pattern to be matched against this string, use the following line to create the pattern:

pattern = /h/i;

This pattern is to find out the existence of the character “h” without case-sensitivity. After that, apply the test method on the pattern, and in the argument of the test method, simply pass the stringVar and store the return value in a new variable as:

result = pattern.test(stringVar);

At the end, simply print the result variable on the terminal using the console log function:

console.log(result);

The complete snippet for this example is as:

stringVar = "Hello World, This is LinuxHint";

pattern = /h/i;

result = pattern.test(stringVar);

console.log(result);

Executing this code will print the following result on the terminal:

The output shows that the character “h” was present inside the stringVar.

Example 2: test() Method With Case Sensitive Pattern

For this example, create a pattern that will search for the character “h” with case sensitivity with the help of the following line of code:

pattern2 = /h/;

After that, create a string variable just like in example 1:

stringVar2 = "Hello World, This is Also LinuxHint";

After that, apply the test() method on the pattern with string value passed inside it:

result = pattern.test(stringVar2);

At the end, pass the result variable in the console log function to print the return value from the test() method on the terminal:

console.log(result);

The complete code snippet for this example would be:

stringVar2 = "Hello World, This is Also LinuxHint";

pattern2 = /h/;

result = pattern.test(stringVar2);

console.log(result);

Executing this code will print the following result on the terminal:

The letter “h” in the small case was not found in the string. Therefore, the result value is false

Wrap up

The RegExp test() is used to match the pattern defined inside the RegExp object against a string and returns a true boolean value if the match is found. Otherwise, a false boolean value is returned. The test() method is applied to a regExp variable with the help of the dot operator. This article has explained the test() method with the help of examples

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.