Skip to content

JavaScript RegExp test() Method

  • by

The test () method in JavaScript’s regular expression (RegExp) object searches a specified string for a match with a given regular expression. If a match is found, the function returns true; otherwise, it returns false.

RegExpObject.test(string)

This method is commonly used for pattern matching and validation in JavaScript.

JavaScript RegExp test() Method example

Simple example code where test() method returns true if the pattern is found in the string, otherwise it returns false

let regex = /hello/;
let str = "hello world";
let result = regex.test(str); // returns true

You can also use the test() method with a global regular expression (one that has the g flag) to search for multiple occurrences of the pattern in the string.

let regex = /hello/g;
let str = "hello world, hello everyone";
let result = regex.test(str); // returns true
result = regex.test(str); // returns true
result = regex.test(str); // returns false

Let’s say you want to create a form where users can enter their email address, and you want to validate whether the email address they enter is in the correct format. You can use a regular expression and the test() method to perform this validation.

function validateEmail(email) {
  let pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return pattern.test(email);
}

let email = "[email protected]";
if (validateEmail(email)) {
  console.log("Email is valid");
} else {
  console.log("Email is invalid");
}

Output:

We create a regular expression pattern that matches the correct format for an email address, using the following pattern:

  • ^ – the start of the string
  • [^\s@]+ – one or more characters that are not whitespace or @
  • @ – the @ symbol
  • [^\s@]+ – one or more characters that are not whitespace or @
  • . – a period
  • [^\s@]+ – one or more characters that are not whitespace or @
  • $ – the end of the string

Here’s an example of the complete HTML code that includes the JavaScript code I provided earlier for validating an email address using the test() method:

<!DOCTYPE html>
<html>
<head>
  <title>Email Validation Example</title>
  <script>
    function validateEmail(email) {
      let pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      return pattern.test(email);
    }

    function handleSubmit(event) {
      event.preventDefault();
      let emailInput = document.getElementById("email");
      let email = emailInput.value;
      if (validateEmail(email)) {
        alert("Email is valid");
      } else {
        alert("Email is invalid");
      }
    }
  </script>
</head>
<body>
  <h1>Email Validation Example</h1>
  <form onsubmit="handleSubmit(event)">
    <label for="email">Email:</label>
    <input type="email" id="email" name="email">
    <input type="submit" value="Submit">
  </form>
</body>
</html>

Comment if you have any doubts or suggestions on this JS Regular expressionJavaScript RegExp topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading