1

I would like to export some single tags that doesn't make sense in my text (exported from MS Word), such as:

<b stuff /> and <i stuff />. 

I've tried the following regex:

/<b(.*?)\/>/i

But it doesn't work when I have something like:

<i>My text</i> some other text<i class="stuff" /> my final text.

Instead of getting only the single tag, it gets everything. How could I fix this, please? The final result must be:

<i>My text</i> some other text my final text.

UPDATE: aelor's answer was the closest to the one I needed. In the end, I mixed both aelor and Mikhail's answer to get this:

/(\s?)<[ib][^>]+\/>(\s?)/

Thanks!

2
  • /<(b|i) (.*?)\/>/i matches both the b and the i. I'd add a space after to prevent removal of other tags. Commented Apr 15, 2014 at 7:16
  • .*? is lazy and will match as little as possible. It's also "match any character zero to infinity times" hence your regex matches include the first tag as well. .+? is lazy "1 to infinity" search. [^>]+ meaning "any character that isn't > matched 1 to infinity times" is much more useful and much better performance wise. Commented Apr 15, 2014 at 7:29

1 Answer 1

2

search <[^>]+\/>

and replace with nothing.

demo here : http://regex101.com/r/xV4xX8

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.