December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 27 | Matching One of Several Characters with the Character Class |
Suppose you want to find a word in a document even if it is misspelled. For example, the word “license” is one of the most misspelled words in English. You want to write a pattern that matches “license,” “licence,” “lisence,” or “lisense” in a document.
Use a character class:
| | const re = /li[sc]en[sc]e/; |
| | |
| | re.test("A driver's license"); // → true |
| | re.test("A driver's lisense"); // → true |
| | re.test("A driver's licence"); // → true |
| | re.test("A shopping list"); // → false |
A character class matches only one out of the specified characters. In this code, the specified characters are “s” ...
Read now
Unlock full access