开发者

Html regex leading and trailing words

开发者 https://www.devze.com 2023-02-11 19:36 出处:网络
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.

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 &l开发者_如何学Pythont;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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消