使用正则表达式删除JavaScript双斜杠(//)样式的注释 [英] Regular expression to remove JavaScript double slash (//) style comments

查看:71
本文介绍了使用正则表达式删除JavaScript双斜杠(//)样式的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过C#中的正则表达式删除JavaScript注释,并且变得卡住了.我想删除任何出现双斜线//样式的注释.

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

我当前的正则表达式是(?<!:)//[^ \ r \ n] * ,它将捕获所有注释并阻止匹配 http://.但是,负面的回望是懒惰的,在下面的测试案例中,我当然被咬了回来:

My current regex is (?<!:)//[^\r\n]* 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 is there maybe a better way to do this?

推荐答案

(根据评论进行了更新)

看起来效果很好:

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

看来,正则表达式英雄中的测试案例证明它将(几乎)按照我认为的方式匹配注释.

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

但是,在注释之前知道如何处理3个或更多引号还不够聪明.我不完全确定如何在不进行硬编码的情况下解决该问题.您需要一些允许偶数引用的方法.

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.

这篇关于使用正则表达式删除JavaScript双斜杠(//)样式的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆