JavaScript

TypeError: regex test is not a function in JavaScript

In JavaScript, the “test()” method executes a search for a match between a regular expression and a given string. It gives “true” if the particular string matches the given pattern; if not, it gives “false”. The test() method can only be used for properly formatted regular expressions. Otherwise, it outputs a TypeError.

This tutorial will define the occurrence and solution of the error “regex test is not a function in JavaScript” TypeError.

How Does the Error “regex test is not a function in JavaScript” Occur?

When you call the “test()” method on a string type value, it will throw an error “regex test is not a function in JavaScript”. In such a scenario, the regex pattern or the regular expression is not wrapped in quotes.

Example
Create a variable “regex” and store a regular expression for performing specific action:

const regex = '\d{10}$';

Create a string to store a number that will be checked against the pattern:

const string = '090078601';

Call the “test()” method by passing the string as an argument to check whether it matched with a pattern or not:

const result = regex.test('string');

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

console.log(result);

The output shows an error because the regex pattern is declared as a string, not in a proper regex format:

How to Fix “regex test is not a function in JavaScript” Error?

To fix the above-mentioned error, call the test() method on the regular expression. The regular expression or the regex pattern declared between two forward slashes. It will act as a string when you declare it in a single or double quote.

In the given example, we will verify whether the number contains 10 digits using the regular expression or regex pattern. First, we will create a variable regex for storing regular expressions:

const regex = /^\d{10}$/;

In the above-given pattern:

  • /” forward slash indicates the start and the end of the pattern.
  • ^” represents the start of the number.
  • d” denotes digits.
  • {}” indicates the limit that is “10”.
  • \” backslash character is the escape character.
  • $” indicates the end of the pattern string.

Create a variable “string” to store the number:

const string = '090078601';

Call the test() method on the regex pattern to test the string:

const result = regex.test('string');

Finally, print the result on the console:

console.log(result);

The output displays “false” because the number is not 10 digits:

We have compiled all the essential instructions to solve the mentioned error.

Conclusion

The specified typeError encounters when calling the “test()” method on a string type value rather than a regular expression or regex pattern. The regular expression or the regex pattern declared between two forward slashes. The regular expression or regex pattern is not wrapped in quotes. Therefore, it acts as a string when you declare it in a single or double quote. In this tutorial, we have defined the occurrence and solution of the error.

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.