Recipe 23Creating Your First Regular Expression

Before using the recipes in Parts 2 and 3, you need to know how to implement regular expressions in your code. In JavaScript, you have two options for creating regular expressions: you can either use the RegExp() constructor or use the regex literal. When using the literal form, you’ll need to enclose the pattern in a pair of forward slash (/) characters, like this:

 const​ re = ​/Hello/​;

This regex pattern is made up of five characters that tell the regex engine to find a direct match for “Hello” in a string. A pattern can be as simple as a single literal character such as “i.” If there are multiple occurrences of “i” in a string, it matches the first one. ...

Get Text Processing with JavaScript 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.