December 2023
Intermediate to advanced
242 pages
5h 28m
English
The test() method returns a Boolean indicating whether or not a pattern exists in the given string. For example, the following pattern checks if the string contains the substring “day”:
| | const str = "12 days"; |
| | const re = /day/; |
| | |
| | console.log(re.test(str)); // → true |
Some regular expression methods such as test() and exec() use the value of lastIndex as the starting position to begin searching the string. If the match succeeds, test() returns true and updates the lastIndex property of the regular expression object. Here’s an example:
| 1: | const str = "10, 20, 30"; |
| - | const re = /\d\d/g; // Match two consecutive digits |
| - | |
| - | console.log(re.test(str)); ... |
Read now
Unlock full access