【问题标题】:Html regex leading and trailing wordsHtml 正则表达式前导词和尾随词
【发布时间】:2011-06-28 21:21:25
【问题描述】:

我有一种感觉,我真的在这里遗漏了一些明显的东西,但我正在寻找一个正则表达式,它可以匹配粗体标签的内容以及标签前后的单词。

所以:

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

会匹配到

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

我可以使用&lt;b\s*&gt;(.*?|[^&gt;\s]+)&lt;\/b\s*&gt; 访问标签及其内容,但我似乎无法确定前导词和尾随词。

任何帮助将不胜感激。

【问题讨论】:

  • 您想要一个正则表达式从start this string &lt;b&gt;is the text&lt;/b&gt; we need end 获取例如string &lt;b&gt;is the text&lt;/b&gt; we
  • 我有点困惑为什么.*?|[^&gt;\s]+ 在中间。它比普通的.*? 买了什么?

标签: html regex tags bold


【解决方案1】:

试试这个(根据你的正则表达式):

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

查看Rubular regex tester

但也许这样会更好:

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

查看Rubular regex tester

【讨论】:

  • 我有点困惑,为什么.*?|[^&gt;\s]+ 在中间。它比普通的.*? 买了什么?
  • @j_random_hacker:据我所知,没什么。但我基于 OP 的正则表达式,它有这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 2014-02-16
相关资源
最近更新 更多