0

I would like a regular expression to find <span>content<span> without any attribute and delete the span tags, but keep the content.

I try: /<(\/)?span([^>]*)>/g

But get all span

<span style="font-weight: bold;">A</span>
<span id="foo" class="foo">B</span>
<span>C</span>

Here an example: https://regex101.com/r/oDIy4J/1

user3483203
  • 50,081
  • 9
  • 65
  • 94
  • 2
    `([^>]*)` is what's picking up the extras. You should be able to get by with just `/<\/?span>/g`. Alternatively, you could also handle it this way: `str = str.replace(/([^<]+)<\/span>/g, "$1")` – jmcgriz Jan 11 '18 at 19:31
  • @jmcgriz can you add that as an answer? – Thomas Smyth - Treliant Jan 11 '18 at 19:48
  • 1
    [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) - just use a parser. – ctwheels Jan 11 '18 at 19:54
  • @ThomasSmyth certainly – jmcgriz Jan 11 '18 at 20:02

1 Answers1

0

([^>]*) is what's picking up the extras. You should be able to get by with just /<\/?span>/g. Alternatively, you could also handle it this way: str = str.replace(/<span>([^<]+)<\/span>/g, "$1")

jmcgriz
  • 2,819
  • 1
  • 9
  • 11
  • This will work as long as there are no tags within the span content (which may in fact be what is wanted). – John Hascall Jan 11 '18 at 20:04
  • @JohnHascall Yeah, OP is explicitly looking for span tags that aren't carrying any attributes – jmcgriz Jan 11 '18 at 20:11
  • I was thinking not of attributes but `blah1blah` and so on – John Hascall Jan 11 '18 at 20:19
  • @JohnHascall True. Yeah that way is less desirable anyway, it makes a lot more sense to use `/<\/span>/g` to just strip the tags rather than trying to mess with the content – jmcgriz Jan 11 '18 at 20:40
  • @JohnHascall What is the solution to this case? – Rodrigo Augusto Bruno Jan 12 '18 at 13:56
  • @RodrigoAugustoBruno In the general case there is no Regex-based solution, as a Regex is basically a Finite automaton, but to do matching requires a more powerful automaton, the Pushdown automaton. The classic "CS" example of this is matching parentheses, but it applies to tags the same. Basically, it comes down to not being able to know in advance the maximal level of nesting you might encounter. A general solution requires a parser (which is a Pushdown automaton). If you can make simplifying assumptions, a Regex may get the job done. – John Hascall Jan 12 '18 at 15:56