简体   繁体   English

正则表达式:跳过引号中的注释

[英]Regex: skip comments within quotes

With this regex /#(.*?)\\r?\\n|#(.*?)$/g am able to parse the content below but it also matches the comment within the quotes. 使用此正则表达式/#(.*?)\\r?\\n|#(.*?)$/g可以解析下面的内容,但它也匹配引号中的注释。

How do I avoid this? 如何避免这种情况?

#
# this is a comment
#

but this is '# not a comment'
and this is "# not a comment either"

# help, please

I tried /(?!\\B["'][^']*)(#(.*?)\\r?\\n|#(.*?)$)(?![^']*['"]\\B)/g but the results are buggy. 我试过/(?!\\B["'][^']*)(#(.*?)\\r?\\n|#(.*?)$)(?![^']*['"]\\B)/g但结果有问题。

Any help? 有什么帮助吗?

Try this simple code to match a comment only at the beginning of a line 尝试使用此简单的代码以仅在行首匹配注释

/^#(.*?)$/gm

Demo: https://regex101.com/r/YngpW9/1/ 演示: https//regex101.com/r/YngpW9/1/

Alternative code to match the comment anywhere 可在任何地方匹配注释的备用代码

/^[^'"]*?(#.*?)$/gm

Demo: https://regex101.com/r/YngpW9/2/ 演示: https//regex101.com/r/YngpW9/2/

Make sure to use gm , not just g , so that you can make use of ^ which matches the beginning of a line. 确保使用gm ,而不仅仅是g ,以便可以使用与行首匹配的^

This is an example 这是一个例子

 var string = ` # # this is a comment # but this is '# not a comment' and this is "# not a comment either" # help, please `; var regex = /^[^'"]*?(#.*?)$/gm; var match = regex.exec(string); while (match != null) { document.write(match[1]+'<br>') match = regex.exec(string); } 

One way to accomplish this is to use capture groups and alternation to distinguish between the contexts you want, and the contexts you don't want. 实现此目的的一种方法是使用捕获组和交替来区分所需的上下文和不需要的上下文。 This is a technique I learned from this article . 这是我从本文中学到的技术。

The trick is to only put the thing you want to match in a capture group, and leave all other alternatives out of the capture group. 诀窍是仅将要匹配的事物放入捕获组,而将所有其他替代方案排除在捕获组之外。 Then you will filter your result matches based on whether or not they have a capture group. 然后,您将根据结果匹配项是否具有捕获组来对其进行过滤。

The regex would look like this: 正则表达式如下所示:

/'(?:\\.|.)*?'|"(?:\\.|.)*?"|#(.*)$/gm

Regex101 Demo Regex101演示

You would use it like this: 您可以这样使用它:

 var re = /'(?:\\\\.|.)*?'|"(?:\\\\.|.)*?"|#(.*)$/gm; var str = ` # # this is a comment # but this is '# not a comment' and this is "# not a comment either" # help, please `; str.replace(re, function(match, group1) { if (group1 !== undefined) { console.log(match); } }); 

You can even extend this to match multi-line comments . 您甚至可以扩展它以匹配多行注释

 var re = /'(?:\\\\.|.)*?'|"(?:\\\\.|.)*?"|(#(.*)$|\\/\\*([\\s\\S]*?)\\*\\/)/gm; var str = ` # # this is a comment # /* this is a multiline comment */ but this is '# not a comment' and this is "# not /* a comment */ either" # help, please `; str.replace(re, function(match, group1) { if (group1 !== undefined) { console.log(match); } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM