© Jörg Krause 2017

Jörg Krause, Introducing Regular Expressions, 10.1007/978-1-4842-2508-0_4

4. Examples of Patterns

Jörg Krause

(1)Berlin, Germany

You have to do some tasks again and again in your life as a developer. This chapter contains several useful examples.

Web and Network

The expressions in this section are dedicated to web and network environments.

HTML Tags

This is how you look for an HTML tag with the fixed name DIV:

/<DIV\b[ˆ>]*>(.*?)<\/DIV>/i

And this expression looks for any tag:

/<([A-Z][A-Z0-9]*)\b[ˆ>]*>(.*?)<\/\1>/i

Once again, I’m using a \1 reference on the first hit to find the matching closing tag.

1   var patt = /<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>/i;2   var html = "Ww want write <b>bold</b> and <i>italic</i> here";3   console.log(patt.exec(html)); ...

Get Introducing Regular Expressions: JavaScript and TypeScript 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.