catch that tag puzzle

the case

  • we need to find regex expression that catches both opening and closing tag of an element, i.e. a single expression that catches all <span> + </span>
  • the objective here is to delete all span tags with possible attributes and keep the content within

the solution

  • <\*span.*?> (or another tag)
    • works for me in Visual Studio Code so far
    • do not autoformat before searching: autoformatting creates newlines (see the details)

the details

  • I started with <span.*>|</span.*> – the only one I found so far
  • but later, I found a better one
    • * – the Kleene star works perfectly for this because it serves the function of OR
    • it matches if / is there and also is not there: it is there in closing tags, it is not there in opening tags
    • it matches if attributes are there but also if they are not there: they can be there in opening attributes, they are not there in closing attributes
    • the ? makes the star lazy (not greedy) which means that it stops matching with the first occurrence of the angle brackets >
  • but if you use autoformat in VSC, you have to make it monstrous:

Leave a Reply

Your email address will not be published. Required fields are marked *