-2

How to select <span> some text </span> excluding <p><span>some text </span></p> using a RegEx?

For a example:

<p><span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur porttitor dignissim feugiat. Ut dui ligula, 

  <span>SELECT THIS ELEMENT, including span tags</span>,

bibendum sed nisi. Donec lectus justo, tempor ac mattis sit amet,
imperdiet at nibh. Maecenas ac laoreet felis. Vestibulum ultricies
mi in diam sagittis in molestie urna porta.</span></p> 

I need select those kind of extra spans and replace or remove it.

Rob W
  • 341,306
  • 83
  • 791
  • 678

2 Answers2

0

One hack if you fail to find straight answer is to first select the full text, then select the inner span text only, and remove that inner span text from the full one. But you need to make sure inner text is unique.

Another way is to strip inner html from full html. This way you eliminate accidental text duplicate, since you will be removing the full inner text

Bashar Abdullah
  • 1,545
  • 1
  • 16
  • 27
  • thanks mate.. Im looking for a way to select them because the text is not unique – Ian Iddamalgoda Dec 24 '11 at 07:21
  • OK, then you can first store content of root span in say full_text, then use regular expression to grab the inner span inner_text = /.*<\/span>/ then simply, using your language, do replace of inner_text from the full_text with empty value. like full_text.replace(inner_text, '') Since you're including '...' in your replace string, it should be unique. there is probably smarter way, but that will get you going – Bashar Abdullah Dec 24 '11 at 16:03
0

I found the solution:

Find/search as this (< p><s pan>)([^>]*)(< span>)([^>]*)(< /span>) and replace it with \1\2\4.

If anyone is copying this he have to put up a space on the beginning of the tags

For almost all tags works for me.

Jorjon
  • 5,316
  • 1
  • 41
  • 58