Reputation: 47
I am writing some code to remove span tags with an specific class from my database. Whenever I remove the opening tag, I need to remove the closing tag as well. For example, I'd like to turn this:
<span class="someClass">Hello</span><span></span>
<span class="someClass">My <span>name</span> is Joe</span>
Into:
Hello<span></span>
My <span>name</span> is Joe
I'm trying to perform this using regex, but I've came to the conclusion that it's not possible. So my second guess was to select only the cases where the content inside the opening and closing tags isn't a span tag itself.
/<span class="someClass">(.*?)<\/span>/g
works well for the first case but would cause problem on the second one. However if I try /<span class="someClass">(.*)<\/span>/g
would cause a problem on the first one.
Is there a way to make a regex that will only get the first case? I want it to ignore only if there are children span tags, which means something like this
<span class="someClass">Hello <a href="#">world</a></span>
would be selected as well.
Upvotes: 1
Views: 260
Reputation: 1480
For this solution to work, you'll need to consider whole string as a single line, maybe with s
(singleline) option.
/<span((?!>).)*>((?!<span).)*?<\/span>/s
Upvotes: 1