【发布时间】:2014-09-02 08:45:10
【问题描述】:
在java中使用正则表达式,我想检测以//开头的单行JS注释。所以我想出的是-
[^:]\/\/.*$
上面的正则表达式没有捕获:
http://example.com
但是它从以下字符串中捕获了高亮部分(我可以理解为什么):abc//qqqqqqqqqq 。但我不希望捕获 c(字符紧邻 //)。
另外,我根本不想捕获下面的字符串:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%//Table for help essages ends%>
我知道,这个话题被多次讨论。但没有什么对我有太大帮助。所以我再次问这个问题。出于测试目的,我使用http://rubular.com/这个站点。
谁能帮帮我。
解决办法:
(?<!http:|https:)\/\/.*(?<!>)\s$
要删除我们正在使用的其他类型的 cmets:
<!--(.|\s)*?-->
\/\*(.|\s)*?\*\/
<%--(.|\s)*?--%>
测试用例:
http://example.com
abc // qqqqqq>qqqq
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<% // END: Modified for Bug # 1070 %>
// comment
/* comment */ program //comment
var ok = "not really";
// This is a comment
// Shouldn't this be a comment too?
var one = 't "stuff"\' now.'; // comment /* hola */ // lol
/* multiline comment
// still
/* still * * * * / */
something here
'string\' // string'; // comment /* comment
/regex/; // comment */* still-a-comment
' /**/ string ' /* "comment..."
// still-a-comment */ alert('isn\'t a comment!');
/\/* this isn't a comment! */; //* comment
/*
//a comment... // still-a-comment
12345
"Foo /bar/ ""
*/
/*//Boo*/
/*/**/
【问题讨论】:
-
如果 Java 支持,请使用否定的后视。
-
cmets前面有冒号吗?