【问题标题】:Regexp match strickly html comment string正则表达式严格匹配 html 注释字符串
【发布时间】:2012-07-04 23:53:00
【问题描述】:

我正在尝试使用正则表达式从 .html 文件中删除所有 html cmets。 我们知道html cmets格式是<!-- some comments --> 我已经创建了这样的正则表达式

/<!--.*-->/gs

它可以工作,但如果文件中有多个注释块,它不会将任何一个块剥离到另一个块,而是从第一个 <!-- 到最后一个 --> F.e.

some html tags
<!-- some comments 1 -->
some html tags 2
<!-- some comments 2-->

它剥离整个

<!-- some comments 1 -->
some html tags 2
<!-- some comments 2-->

我是使用 ActionScript 语言的代码。 任何意见将是有益的。 谢谢!

【问题讨论】:

  • 你需要.+?,而不是.*

标签: regex comments strip


【解决方案1】:

使用这个正则表达式/&lt;!--.*?--&gt;/gs

【讨论】:

  • 这不会匹配多行 cmets。
【解决方案2】:

用问号让星号变懒:

/<!--.*?-->/gs

【讨论】:

  • 谢谢!它现在有效!你能解释一下这个问号对这个正则表达式有什么作用吗?
  • 问号使星号字符lazy。就像“在停止之前尽可能少做工作”。
猜你喜欢
  • 2012-10-26
  • 2011-10-29
  • 2010-11-14
  • 2017-03-20
  • 2019-04-10
  • 2012-06-05
  • 2013-12-25
相关资源
最近更新 更多