• JavaScript Video Tutorials

JavaScript - RegExp test Method



The JavaScript RegExp.test() method tests whether the given string matches a specified pattern defined by the regular expression. It returns a boolean value 'true' if the match is found; otherwise, it returns a 'false' value.

In JavaScript, a regular expression (RegExp) is an object that describes a sequence of characters used for defining a search pattern.

Syntax

Following is the syntax of JavaScript RegExp.test() method −

RegExp.test(str)

Parameters

This method accepts a parameter named 'str', which is described below −

  • str − The string in which we want to search for a match.

Return value

This method returns 'true' if a match is found between regex and the string, 'false' otherwise.

Examples

If the match is found, it will return 'true'.

Example 1

In the following program, we are using the JavaScript RegExp.test() method to test whether the string matches a specified pattern ("\Hello*") defined by the regular expression within this string "Hello World".

<html>
<head>
   <title>JavaScript RegExp.test() Method</title>
</head>
<body>
   <script>
      const str = "Hello World";
      const regex = new RegExp("\Hello*");
      document.write("The given string: ", str);
      document.write("<br>The regex: ", regex);
      let bool = regex.test(str);
      document.write("<br>Does the match found? ", bool);    
   </script>
</body>
</html>

Output

The above program returns 'true'.

The given string: Hello World
The regex: /Hello*/
Does the match found? true

Example 2

If there is not match found, it will return 'false'.

The following is another example of the JavaScript RegExp.test() method. We use this method to test whether the string matches the specified pattern ("\html*", 'g') defined by the regular expression within this string "JavaScript Regular Expression".

<html>
<head>
   <title>JavaScript RegExp.test() Method</title>
</head>
<body>
   <script>
      const str = "JavaScript Regular Expression";
      const regex = new RegExp("\html*", 'g');
      document.write("The given string: ", str);
      document.write("<br>The regex: ", regex);
      let bool = regex.test(str);
      document.write("<br>Does the match found? ", bool);    
   </script>
</body>
</html>

Output

After executing the above program, it will return 'false'.

The given string: JavaScript Regular Expression
The regex: /html*/g
Does the match found? false

Example 3

Using RegExp.test() method on a regex with the global flag "g".

In the example below, we use the JavaScript RegExp.test() method to test whether the mobile numbers "8429618353" and "9511277492s" are valid or not.

<html>
<head>
   <title>JavaScript RegExp.test() Method</title>
</head>
<body>
   <script>
      const mob1 = "8429618353";
      const mob2 = "9511277492s";
      var regex = new RegExp("^[0-9]{10}$", "g");
      document.write("The given mobile numbers are : ", mob1 , " and ", mob2);
      document.write("<br>The regex: ", regex);
      let bool1 = regex.test(mob1);
      let bool2 = regex.test(mob2);
      document.write("<br>Does mobile number ", mob1, " is valid? ", bool1);
      document.write("<br>Does mobile number ", mob2, " is valid? ", bool2);   
   </script>
</body>
</html>

Output

Once the above program is executed, it will display the following statements −

The given mobile numbers are : 8429618353 and 9511277492s
The regex: /^[0-9]{10}$/
Does mobile number 8429618353 is valid? true
Does mobile number 9511277492s is valid? false
Advertisements