Html regex leading and trailing words

197 views Asked by At

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.

1

There are 1 answers

2
Sander Marechal On BEST ANSWER

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