Question

I have a feeling that I'm really missing something obvious here but I'm looking for a regex that will match the content of a bold tag plus the words immediatly before and after the tags.

So:

"start this string <b>is the text</b> we need end"

would match to

"string <b>is the text</b> we"

I can get to the tags and their content with <b\s*>(.*?|[^>\s]+)<\/b\s*> but I can't seem to nail down the leading and trailing words.

Any help would be appreciated.

Was it helpful?

Solution

Try this (based on your regex):

/\w+\s*<b\s*>(?:.*?|[^>\s]+)<\/b\s*>\s*\w+/

See it on Rubular regex tester

But perhaps this would be better:

/\w+\s*<b\s*>.*?<\/b\s*>\s*\w+/

See it on Rubular regex tester

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top