简体   繁体   中英

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

But it is not working with multiline comments ...

It makes sense to replace the comments from text with \\/\\*.*?\\*\\/ instead.

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

Strip out comments

Remove every css comments (in or extra block and with stars * inside) using the replace function with the following regex:

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

Regex Breakout Regex101 Demo

/                  # 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 than the code proposed in the other answer with the lazy modifier:

Live Js Demo

 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() .

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. Only a proper parser, like Esprima , can do that correctly.

The easiest way for you is to use the code/content with all its comments removed.

For that you can use library decomment . It guarantees correct comments removal, as it relies on Esprima to identify all the regular expressions:

var decomment = require('decomment');

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

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

For build systems / task runners see gulp-decomment and grunt-decomment .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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