Special Characters and Escaping

Now that you’ve met some of the super-powerful characters of Regular Expression Land, you might want to create a regular expression that matches strings that contain those amazing characters, just to show what a huge fan you are. Give it a shot and check out Listing 6.8.

Listing 6.8 Looking for Stars, Plus More

> var starPattern = /*/;  SyntaxError: Unexpected token ILLEGAL> var plusPattern = /+/;  SyntaxError: Invalid regular expression: /+/: Nothing to repeat

Well, that didn’t go well. The first SyntaxError is pretty mysterious, but the second error is a bit more helpful. Our plusPattern regular expression is invalid because the + has no preceding character that should be repeated. ...

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.