regex - Regular expression to get span tag -


this question has answer here:

may know regular expression rule matching html tag test

that matches tag span regardless property in it.

my existing rule this, not working.

/(<span [^>]*>)>/s 

thanks.

david, reason (<span [^>]*>)> have small typo.

you see, expression tries match two closing >: closely @ end >)>. instance, match <span hey there>> not <span hey there>

to match opening span, make sure have 1 >.

with disclaimers using regex match html, regex do:

<span[^>]*> 

if expect span, make sure make case-insensitive.

only if have time: additional flourish

in comment, @davidehrmann points out regex above match <spanner>. if want make him happy , ensure if span more <span> contains space after span, can use:

<span(?: [^>]*)?> 

however, in view, unnecessary flourish. when parse html regex, know using crude tool, , rely on input well-formed. instance, revised regex above, there still million ways can match improper html, instance: <span classification>

what do? nothing. know tools, know can do, know risks, , decide when situation warrants regex , when warrants dom parser.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -