Repetition

The regular expression namePattern isn’t much of a pattern at all; it’s no different than using Find. What if I want to match Steven or Steve? Listing 6.4 shows how inflexible namePattern is.

Listing 6.4 Trying to Match Steve

> var myNickname = 'Steve Foote';  "Steve Foote"> namePattern.test(myNickname);  false

Dang. My regular expression isn’t flexible enough for even my nickname. We can fix that—but be warned, this is where regular expressions start to look weird.

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.