Last updated: Mar 2, 2024
Reading timeยท3 min
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)
.
Here is an example of how the error occurs.
const str = {}; // โ๏ธ TypeError: match is not a function const result = str.match(/[A-Z]/g);
We called the String.match method on an object and got the error back.
match()
method on stringsTo solve the error, console.log
the value you're calling the match
method on
and make sure to only call the method on strings.
const str = 'Bobby Hadz'; const result = str.match(/[A-Z]/g); console.log(result); // ๐๏ธ ['B', 'H']
If the value is not a string, you can convert it using the String()
constructor.
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.
match()
Alternatively, you can check if the value is a string before calling the
match()
method.
const str = null; const result = typeof str === 'string' ? str.match(/[0-2]/g) : null; console.log(result); // ๐๏ธ null
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.
const str = undefined; let result = null; if (typeof str === 'string') { result = str.match(/[0-2]/g); } console.log(result); // ๐๏ธ null
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.
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.
// โ 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' ]
We accessed a property on the object and an element in the array before calling
the String.match()
method.
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')
.
Here is an example of how the error occurs.
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.
test()
method on regular expressionsTo resolve the issue, make sure to only call the test()
method on regular
expressions.
const result = /[a-z]/.test('example'); console.log(result); // ๐๏ธ true
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.
When storing the regular expression in a variable, make sure to not wrap it in quotes.
const regex = /[a-z]/; const result = regex.test('example'); console.log(result); // ๐๏ธ true
If the regex is wrapped in quotes, it becomes a string and strings don't
implement a test()
method.