Match Anything, Period

Sometimes you want a pattern that matches anything. For instance, we could make our excitementPattern match only strings that contain some form of Wow and contain an exclamation point (!), but there may be some stuff between the Wow and the ! that we don’t really care about. Regular expressions have another super-powerful special character to handle that situation: The period, usually called the dot (as in dot-com), matches any character. It’s like the regular expression wildcard. You can combine the dot and the star to match zero or more characters, regardless of what those characters are. See Listing 6.10.

Listing 6.10 Matching Anything

> excitementPattern = /Wo*w.*!/;  /Wo*w.*!/> excitementPattern.test(notYetExcited); ...

Get Learning to Program now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.