[英]Select everything except comments
我知道如何從文本中刪除評論,但我正在尋找不同的東西。
我想選擇除代碼塊之外的所有內容。
我使用了以下正則表達式:
/^(?!(\/\*([\s\S]+)\*\/)).*/gm
您可以在此處查看: https : //regex101.com/r/cN5aT1/2
但它不適用於多行評論......
這是有道理的,以replace
從文與評論\\/\\*.*?\\*\\/
來代替。
雖然如果您確實需要它並假設這不會進入您的生產代碼,您可以嘗試
/([^*\/][^*\/][^*\/]*)(?=\/\*.*?\*\/|$)/gs
刪除評論
使用帶有以下正則表達式的replace
函數刪除每個css注釋(in或extra block以及里面的星*
):
/\s*[/][*](?:[^*]|[*][^/])*[*][/][ \t]*/g
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
關於性能 :下面的代碼比使用延遲修飾符的其他答案中提出的代碼快14倍 :
現場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>
獲取不應與特定模式匹配的文本的快速簡便的解決方案是使用String#split()
。
所以,我知道匹配/* */
評論的最佳正則表達式是
/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//g
然后,用正則表達式分割輸入:
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>";
在JavaScript中檢測注釋並不是一個簡單的解決方案,您不能單獨依賴RegEx。 只有正確的解析器,如Esprima ,才能正確完成。
最簡單的方法是使用刪除了所有注釋的代碼/內容。
為此你可以使用庫decomment 。 它保證正確刪除注釋,因為它依賴於Esprima來識別所有正則表達式:
var decomment = require('decomment');
var code = "var t; // comments";
decomment(code); //=> var t;
對於構建系統/任務運行者,請參閱gulp-decomment和grunt-decomment 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.