Capture the Tag

Now that you’ve seen a bit about how using regular expressions and capturing groups work, it’s time to try some out on your own. Open the Chrome Dev Tools on any page (I recommend the Mozilla Developer Network, but any page will do). Find an HTML tag in the Elements tab, copy the HTML of that element (see Figure 6.6), and save the string to a variable in the console (I recommend an element that has no children so that you don’t have to deal with newlines when saving the string to a variable). Now write a regular expression that will capture the tag name (for example, the tag name of <h2> is h2). You should be able to use the same regular expression to capture the tag name of any HTML tag you copy.

Figure 6.6 Copy the HTML of ...

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.