JS regex pattern help wanted

时间:2017-06-15 10:00:36

标签: regex

I'm writing a Javascript minifier in python, and I'm trying to remove comments by using a regular expression

\s\/\/[^\n]*\n

The javascript that is giving me unexpected results from the regex is

/* deliberately different */
// test line 1
// test line 2
var test_http = "http://test.com";

I'm using the great regex testing resource at Regex101 and it's showing that // test line 2 is not matched, and I don't understand why.
NOTE: I am deliberately looking for white-space (includes newlines) before a comment in order NOT to match declarations such as the test_http variable.

Can anyone shed some light on this for me please?

1 个答案:

答案 0 :(得分:0)

  

我故意在评论之前寻找空格(包括换行符),以便不匹配诸如test_http变量之类的声明。

如果我们可以假设每条评论都会从一个新行开始,那么这种模式就可以了:

^(?:(?:/\*(?:.|\n)*?\*/)|(?://.*))

Live Demo

但是,如果不是这种情况,您将不得不花费一些时间来开发一个上下文感知解析器。这是因为正则表达式从未被设计为具有上下文感知能力。如果你想根据匹配的上下文采取不同的行动,单独使用正则表达式(通常)就足够了。

此模式可与上下文感知解析器结合使用以识别注释:

(?:/\*(?:.|\n)*?\*/)|(?://.*)

Live Demo