?

Leave behind any preconceptions you might have about what the question mark means. In the land of regular expressions, unassuming characters can leave behind the roles they play in normal life and take on new, meaningful powers. The simple ? has the power to solve our nickname problem. See Listing 6.5.

Listing 6.5 The Powerful ?

> namePattern = /Steven?/;  /Steven?/> namePattern.test(myNickname);  true> namePattern.text(myName);  true

Magic! The ? tells the regular expression that the character before the ? should appear once or not at all. In other words, the new namePattern says that the n is optional.

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.