Running Regular Expressions

When we want to actually match our pattern against a string, we use the exec() method. This method takes the pattern and compares it to the string. The result is placed in another object (which is also be an array).

foundMatch = re.exec(ordString)

We can also take a shortcut and simply code this:

foundMatch = re(ordString)

but it doesn't work in all browsers.

Information about the match is stored in the foundMatch object. Information about the match is also stored in the re object and in the RegExp object. When you use regular expressions, information is spread all over the place. Here's some code in Example 17-2 to spell out what all the various property values are.

Example 17-2. Seeing all property values
 <html> ...

Get Advanced JavaScript™: Insights and Innovative Techniques 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.