Using Regular Expressions in JavaScript
Regular expressions do nothing without a string to test them on. Let’s start with a simple string and a simple regular expression. Open the Chrome Dev Tools on any page, and create a string with your name in it; then create a regular expression with your name in it. See Listing 6.2.
> var myName = 'Steven Foote'; "Steven Foote"> var namePattern = /Steven/; /Steven/
Now you need to test whether the string matches the regular expression. You can do this in a few different ways, each with a slightly different purpose and output.
test
returns a Boolean, true
if the string matches and false
if it doesn’t.
exec
either returns an ...
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.