简体   繁体   English

选择除评论之外的所

[英]Select everything except comments

I know how to remove comments from text but i'm looking for something different. 我知道如何从文本中删除评论,但我正在寻找不同的东西。

I want to select everything except the code block. 我想选择除代码块之外的所有内容。

I used following regex: 我使用了以下正则表达式:

/^(?!(\/\*([\s\S]+)\*\/)).*/gm

You can check it here: https://regex101.com/r/cN5aT1/2 您可以在此处查看: https//regex101.com/r/cN5aT1/2

But it is not working with multiline comments ... 但它不适用于多行评论......

It makes sense to replace the comments from text with \\/\\*.*?\\*\\/ instead. 这是有道理的,以replace从文与评论\\/\\*.*?\\*\\/来代替。

Though if you do want it and assuming that this will not go into your production code you can try 虽然如果您确实需要它并假设这不会进入您的生产代码,您可以尝试

/([^*\/][^*\/][^*\/]*)(?=\/\*.*?\*\/|$)/gs

https://regex101.com/r/cN5aT1/6 https://regex101.com/r/cN5aT1/6

Strip out comments 删除评论

Remove every css comments (in or extra block and with stars * inside) using the replace function with the following regex: 使用带有以下正则表达式的replace函数删除每个css注释(in或extra block以及里面的星* ):

/\s*[/][*](?:[^*]|[*][^/])*[*][/][ \t]*/g

Regex Breakout Regex101 Demo Regex Breakout Regex101演示

/                  # Regex start delimiter
\s*                # select any whitespace char (including the newlines preceding the comment)
[/][*]             # match comment start delimiter '/*'
(?:[^*]|[*][^/])*  # select zero or more not star ('*') chars (also newlines) 
                   # or a star and the following char (that is not '/')
[*][/]             # match comment stop delimiter '*/'
[ \t]*             # match any space or tabs (no newlines to preserve indentations)
/g                 # Regex close delimiter and global flag

About performance : The code below is 14 times faster than the code proposed in the other answer with the lazy modifier: 关于性能 :下面的代码比使用延迟修饰符的其他答案中提出的代码快14倍

Live Js Demo 现场Js演示

 var filter = /\\s*[/][*](?:[^*]|[*][^/])*[*][/][ \\t]*/g; var input = '.class-n {\\n\\n}\\n\\n/* Select everything but these comments */\\n\\n.class-1 {\\n font-family: \\'SourceCodePro\\';\\n font-size: 16px; /* a comment with\\n **stars***** */\\n line-height: 18px;\\n}\\n\\n/* Select everything but these comments */\\n\\n.class-2 {\\n background-color: rgb(40, 40, 40); /* another\\n inline comments that\\nspans on multiple lines */\\n border: 1px solid rgb(20, 20, 20);\\n padding: 10px 15px 10px 15px;\\n}\\n\\n/* Single line */\\n\\n/* Multiline\\n Comment */\\n'; var output = input.replace(filter,''); document.getElementById('input').innerHTML += '<xmp>' + input + '</xmp>'; document.getElementById('output').innerHTML += '<xmp>' + output + '</xmp>'; 
 .flexbox-container { display: -ms-flex; display: -webkit-flex; display: flex; } .flexbox-container > div { width: 50%; padding: 15px; border:1px solid black; } .flexbox-container > div:first-child { margin-right: 15px; } 
 <div class="flexbox-container"> <div id="input"><h3>Original Css</h3></div> <div id="output"><h3>Stripped Css</h3></div> </div> 

A very quick and easy solution to get text that should not be matched with a specific pattern is using String#split() . 获取不应与特定模式匹配的文本的快速简便的解决方案是使用String#split()

So, the best regex to match /* */ comments I know is 所以,我知道匹配/* */评论的最佳正则表达式

/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//g

Then, just split the input with the regex: 然后,用正则表达式分割输入:

 var re = /\\/\\*[^*]*\\*+([^\\/*][^*]*\\*+)*\\//g; var s = '.class-n {\\n\\n}\\n\\n/* I want to select everything but these comments */\\n\\n.class-1 {\\n font-family: \\'SourceCodePro\\'; \\n font-size: 16px;\\n line-height: 18px;\\n}\\n\\n/* I want to select everything but these comments */\\n\\n.class-2 {\\n background-color: rgb(40, 40, 40);\\n border: 1px solid rgb(20, 20, 20);\\n padding: 10px 15px 10px 15px;\\n}\\n\\n/* Single line */\\n\\n/* Multiline\\n Comment */\\n\\nSome text here'; var res = s.split(re).filter(Boolean); document.body.innerHTML += "<pre>"+JSON.stringify(res, 0, 4)+"</pre>"; 

Detecting comments in JavaScript isn't a trivial solution, you cannot rely on RegEx alone for that. 在JavaScript中检测注释并不是一个简单的解决方案,您不能单独依赖RegEx。 Only a proper parser, like Esprima , can do that correctly. 只有正确的解析器,如Esprima ,才能正确完成。

The easiest way for you is to use the code/content with all its comments removed. 最简单的方法是使用删除了所有注释的代码/内容。

For that you can use library decomment . 为此你可以使用库decomment It guarantees correct comments removal, as it relies on Esprima to identify all the regular expressions: 它保证正确删除注释,因为它依赖于Esprima来识别所有正则表达式:

var decomment = require('decomment');

var code = "var t; // comments";

decomment(code); //=> var t;

For build systems / task runners see gulp-decomment and grunt-decomment . 对于构建系统/任务运行者,请参阅gulp-decommentgrunt-decomment

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

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