【问题标题】:Regex only match multiline Comments正则表达式仅匹配多行注释
【发布时间】:2016-10-04 10:05:56
【问题描述】:

我正在尝试提出一个匹配字符串中所有多行 cmets 的正则表达式。问题是,我不允许以相同格式匹配单行 cmets。

匹配:

/* This 
is a 
multiline 
comment 
*/

不匹配:

/* This is a single line comment */

【问题讨论】:

  • 你尝试了什么?
  • 匹配任何多行注释,然后检查它是否包含换行符。
  • 修复了答案中的一些错误。如果您已经检查过,请再试一次。
  • 完美运行。在运行 JUnit 测试之前,我试图清理我的代码。为了确保测试正常工作,我不得不删除所有多行注释。

标签: regex


【解决方案1】:

类似的东西(根据您使用的语言进行额外的更正):

\/\*((?!\*\/)[^\r\n])*[\r\n]((?!\*\/)[\s\S\r\n])*\*\/

测试:

console.log(document.querySelector('textarea').value.match(/\/\*((?!\*\/)[^\r\n])*[\r\n]((?!\*\/)[\s\S\r\n])*\*\//g).join("\n\n"))
textarea { width: 100%; }
<textarea>Match: /* This 
is a 
multiline 
comment 
*/

No Match: /* This is a single line comment */

Match: /* 
This is a multiline ** comment */

No Match: /* This is a single line comment */

Match: /* This is a multiline ** comment 
*/</textarea>

【讨论】:

    猜你喜欢
    • 2013-04-20
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2011-10-29
    • 2010-11-14
    • 2014-04-18
    • 2012-10-12
    • 2018-01-07
    相关资源
    最近更新 更多