0

I need a little help to write a regular expression which can find a span tag if it has inline style in it.

So far I have got ]style=[\"'][^\"'][\"']*|/)?> which does find a span tag with inline style. It can detect the span tag only when there is inline style in it but not the matching ending span tag Please see the screenshot which shows what all it detects in a sample textenter image description here

As you can see the screenshot, the first thing it detects is "" which I don't want. I want to work only when there is an inline style present in the span tag and its corresponding end span tag.

Can someone please help me what change I need to make to get what I want?

The idea here is I am trying to strip out the tag only when there is inline style present in it but if there is a span tag with a class then it is fine.

aspnetdeveloper
  • 629
  • 1
  • 7
  • 16

1 Answers1

2

Soapbox

We could craft a regex to match your specific case, but given this is HTML parsing there could be any number of edge cases which not picked up by a regex. You'd be best off using the DOM or using a product like HTML Agility (free)

However

If you have a basic need to capture the match then you could try:

((<span\b[^>]*\s\bstyle=(["'])([^"]*)\3[^>]*>)(.*?)</span>)

I'm populating the following groups:

  1. Gets the entire string from start to end tag
  2. gets the entire open tag
  3. gets the open single/double quote for the style value. This is probably useless outside of the regex, but I'm using that ensure I capture the correct closing qoute at the end of the value string at Ref 1.
  4. gets the value found in the style key/value set
  5. gets all characters inside the span tag.

Note this will break if there are nested span tags. enter image description here

enter image description here

Community
  • 1
  • 1
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43