Regex to remove javascript double slash (//) style comments

I'm trying to do remove javascript comments via regex in C# and have become stuck. I want to remove any occurrences of double slash // style comments.

My current regex is (?<!:)//[^rn]* which will catch all comments and prevent matching of http:// . However, the negative lookbehind was lazy and of course bit me back in the following test case:

var XSLPath = "//" + Node;

So I'm looking for a regular expression that will perform a lookbehind to see if an even number of double quotes ( " ) occurs before the match. I'm not sure if this is possible. Or maybe there's a better way to do this?


(Updated based on comments)

It looks like this works pretty well:

(?<=".*".*)//.*$|(?<!".*)//.*$

It appears that the test cases in Regex Hero show that it'll match comments the way I think it should (almost).

For instance, it'll completely ignore this line:

var XSLPath = "//" + Node;

But it's smart enough to match the comment at the end of this line:

var XSLPath = "//"; // stuff to remove

However, it's not smart enough to know how to deal with 3 or more quotation marks before the comment. I'm not entirely sure how to solve that problem without hard-coding it. You need some way to allow an even number of quotes.

链接地址: http://www.djcxy.com/p/20066.html

上一篇: 使用Json.net将json对象反序列化为动态对象

下一篇: 正则表达式删除JavaScript的双斜线(/)风格的评论