TypeError: regex match is not a function in JavaScript [Fix]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# Table of Contents

  1. TypeError: regex match is not a function in JavaScript
  2. TypeError: regex test is not a function in JavaScript

# TypeError: regex match is not a function in JavaScript

The "TypeError: match is not a function" error occurs when the match method is called on a value that is not of type string.

To solve the error, make sure to only call the match method on strings, e.g. 'ABC'.match(/[A-Z]/g).

typeerror regex match is not a function

Here is an example of how the error occurs.

index.js
const str = {}; // โ›”๏ธ TypeError: match is not a function const result = str.match(/[A-Z]/g);

regex match is not a function

We called the String.match method on an object and got the error back.

# Only call the match() method on strings

To solve the error, console.log the value you're calling the match method on and make sure to only call the method on strings.

index.js
const str = 'Bobby Hadz'; const result = str.match(/[A-Z]/g); console.log(result); // ๐Ÿ‘‰๏ธ ['B', 'H']

only call match method on strings

The code for this article is available on GitHub

If the value is not a string, you can convert it using the String() constructor.

index.js
const num = 1234; // ๐Ÿ‘‡๏ธ use String() to convert to string first const result = String(num).match(/[0-2]/g); console.log(result); // ๐Ÿ‘‰๏ธ ['1', '2']

The String() constructor converts the supplied value to a string and returns the result.

# Check if the value is a string before calling match()

Alternatively, you can check if the value is a string before calling the match() method.

index.js
const str = null; const result = typeof str === 'string' ? str.match(/[0-2]/g) : null; console.log(result); // ๐Ÿ‘‰๏ธ null

check if value is string before calling match

The code for this article is available on GitHub

We used a ternary operator to check if the str variable stores a string.

If it does, the value to the left of the comma is returned, otherwise, the value to the right is returned.

You could also use an if statement to achieve the same result.

index.js
const str = undefined; let result = null; if (typeof str === 'string') { result = str.match(/[0-2]/g); } console.log(result); // ๐Ÿ‘‰๏ธ null
If the value is a string, we return the result of calling the match method on it, otherwise, we return a null value.

If the error persists, console.log the value you're calling the match method on and check its type using the typeof operator.

index.js
console.log(typeof 'bobbyhadz.com'); // ๐Ÿ‘‰๏ธ string console.log(typeof 12345); // ๐Ÿ‘‰๏ธ number console.log(typeof []); // ๐Ÿ‘‰๏ธ object

If the value is an object, there's a very good chance that you are forgetting to access a specific property on which you need to call the match() method.

index.js
// โœ… with objects const obj = { example: '123456', }; const result1 = obj.example.match(/[0-2]/g); console.log(result1); // ๐Ÿ‘‰๏ธ [ '1', '2' ] // -------------------------------------------- // โœ… with arrays const arr = ['123456', '45', '74']; const result2 = arr[0].match(/[0-2]/g); console.log(result2); // ๐Ÿ‘‰๏ธ [ '1', '2' ]
The code for this article is available on GitHub

We accessed a property on the object and an element in the array before calling the String.match() method.

# TypeError: regex test is not a function in JavaScript

The "test is not a function" error occurs when the test() method is called on a value that is not a regular expression, e.g. a string.

To solve the error, make sure to only call the test method on regular expressions, e.g. /[a-b]/.test('abc').

typeerror regex test is not a function

Here is an example of how the error occurs.

index.js
const regex = '[a-z]'; // โ›”๏ธ TypeError test is not a function const result = regex.test('example');

We called the RegExp.test method on a string and got the error back.

# Only call the test() method on regular expressions

To resolve the issue, make sure to only call the test() method on regular expressions.

index.js
const result = /[a-z]/.test('example'); console.log(result); // ๐Ÿ‘‰๏ธ true

only call test method on regular expression

The code for this article is available on GitHub
The test method can only be called on valid regular expressions. The method checks if there is a match between the regular expression and the string and returns the result.

Notice that the regular expression is not wrapped in a string.

The forward slashes / / mark the beginning and end of the regex.

# Don't wrap regular expressions in quotes

When storing the regular expression in a variable, make sure to not wrap it in quotes.

index.js
const regex = /[a-z]/; const result = regex.test('example'); console.log(result); // ๐Ÿ‘‰๏ธ true
The code for this article is available on GitHub

If the regex is wrapped in quotes, it becomes a string and strings don't implement a test() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev