Over a million developers have joined DZone.

Match An Html Attribute

·
The goal of this expression is to match all "id" attributes of the "div" tags on a page


)
  .
)* # Consume everything until finding " id=" or ">"
   # (">" is just for failing faster)

\sdiv=(?P<__quote>['"])? # Consume " div=" and save the quote type (' or ") if any

(?: # while
  (?! # next character isn't, 
    (?(__quote) # if a quote has been opened,
      (?P=__quote) # the closing quote ;
      |[\s>] # a space or ">" else,
    )
  )
  . # consume this character
)*


(?(__quote) # If we got a quote
  (?P=__quote)| # it must be closed
  (?=[\s>]) # else, the attribute is ended by a space or ">"
)

[^>]*> # Consume the rest of the tag
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}