Javascript 注释剥离器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3577767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript comment stripper
提问by Andrea
I'm looking for some tool to remove cooments from Javascript sources. I was able to Google some, but none of them satisfied the following requirement: Everything else should be left as it is, in particular white space is not removed, BUT if a comment takes a whole line, the line is removed too.
我正在寻找一些工具来从 Javascript 源中删除 cooments。我可以用谷歌搜索一些,但没有一个满足以下要求:其他所有内容都应保持原样,特别是不删除空格,但如果注释占用整行,则该行也将被删除。
Shortly, I want to be able to go from a nicely formatted source with comments to an equally formatted source without comments. Lines which only contain comments are removed, and traliing comments are removed together with the trailing spaces. All the rest is left as it is.
很快,我希望能够从带有注释的格式良好的源转换为不带注释的同样格式的源。仅包含注释的行将被删除,尾随注释与尾随空格一起被删除。其余的都保持原样。
Do you know any tool for such a job?
你知道做这样的工作有什么工具吗?
EDIT: I try to be more specific. Using regular expressions is not possible, as the characters //or /*can also appear inside strings, regular expressions and so on.
编辑:我尝试更具体。使用正则表达式是不可能的,因为字符//或/*也可以出现在字符串、正则表达式等中。
The tool should take this input
该工具应该接受这个输入
var a = true;
//the following code is every useful
var b = 2;//really, really useful
/**
Never, ever do this
var c = 3;
*/
var d = 4;
and give this output
并给出这个输出
var a = true;
var b = 2;
var d = 4;
采纳答案by Thomas Eding
Here's some code I whipped up: Check it out: here
这是我编写的一些代码:检查一下:here
Also hereis an example of my code you can test RIGHT NOW in a webpage
另外这里是我的代码示例,您可以立即在网页中进行测试
Here's one I didn't write that could be handy, though his code will fail on certain regex literals: http://james.padolsey.com/javascript/removing-comments-in-javascript/
这是我没有写的可能很方便,尽管他的代码会在某些正则表达式上失败:http: //james.padolsey.com/javascript/removing-comments-in-javascript/
EDIT: The code I wrote is as is. I am not updating it as it is something I wrote when I was a teenager and rather new to programming. If there is a bug, you can fix it.
编辑:我写的代码是原样的。我不会更新它,因为它是我十几岁的时候写的,对编程还很陌生。如果有错误,您可以修复它。
回答by Sean Vieira
Use Google's Closure Compilerwith WHITE_SPACE_ONLY and PRETTY_PRINT -- the only thing that it will do is remove the comments (Unless of course you don't format your code in the way that PRETTY_PRINT does.)
将 Google 的Closure Compiler与 WHITE_SPACE_ONLY 和 PRETTY_PRINT 一起使用——它唯一能做的就是删除注释(当然,除非您没有像 PRETTY_PRINT 那样格式化代码。)
It turns this:
它变成了这样:
// This function alerts a name
function hello(name) {
/**
* One lone
* multi-line
* comment
*/
alert('Hello, ' + name);
}
hello('New user');
Into this:
进入这个:
function hello(name) {
alert("Hello, " + name)
}
hello("New user");
回答by mpowered
Found a pretty sweet solution here: http://blog.ostermiller.org/find-comment
在这里找到了一个非常好的解决方案:http: //blog.ostermiller.org/find-comment
Excerpt:
摘抄:
Now we just need to modify the comment end to allow any number of *:
现在我们只需要修改注释末尾以允许任意数量的 *:
/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/
We now have a regular expression that we can paste into text editors that support regular expressions. Finding our comments is a matter of pressing the find button. You might be able to simplify this expression somewhat for your particular editor. For example, in some regular expression implementations, [^] assumes the [\r\n] and all the [\r\n] can be removed from the expression.
This is easy to augment so that it will also find // style comments:
我们现在有一个正则表达式,我们可以将其粘贴到支持正则表达式的文本编辑器中。查找我们的评论是按下查找按钮的问题。您可以针对您的特定编辑器稍微简化此表达式。例如,在某些正则表达式实现中,[^] 假定 [\r\n] 和所有 [\r\n] 都可以从表达式中删除。
这很容易扩充,因此它也可以找到 // 样式注释:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
Be sure to read the caveats, however, as this will remove comments from with comments, or can uncomment commented code improperly. Worked perfectly for me, however :-)
但是,请务必阅读注意事项,因为这会从带注释中删除注释,或者可能会不正确地取消注释已注释的代码。然而,对我来说非常适合:-)
回答by vitaly-t
Library decommentdoes exactly what you described:
库decomment完全按照您的描述执行:
Everything else should be left as it is, in particular white space is not removed, BUT if a comment takes a whole line, the line is removed too.
其他所有内容都应保持原样,特别是不会删除空格,但如果注释占用整行,则该行也会被删除。
And it also supports JSON5, JavaScript ES6, CSS and HTML.
并且它还支持 JSON5、JavaScript ES6、CSS 和 HTML。
回答by Trident D'Gao
naive one liner stripper:
天真的单衬剥离器:
var noComments = text.replace(/\/\*(.|[\r\n])*?\*\//g, '').replace(/\/\/.*/gm, '');
DISCLAIMER:
免责声明:
"naive" means:
“天真”的意思是:
it strips across everywhere, say if you have:
var a = "/*"; someImportantLogicHere(); var b = "*/";then you will get
var a = "";order in which you apply these regexps matters, you will get different results applying it in different order
它到处都是,如果你有:
var a = "/*"; someImportantLogicHere(); var b = "*/";然后你会得到
var a = "";应用这些正则表达式的顺序很重要,以不同的顺序应用它会得到不同的结果
but for 95% other cases it's simple and paractical
但对于 95% 的其他情况,它是简单而实用的
回答by Geng Jiawen
You can use babel "comments": falseto achieve this. I have wrote a demo to for the-super-tiny-compiler, please check https://github.com/gengjiawen/the-super-tiny-compiler.
您可以使用 babel"comments": false来实现这一点。我已经为 编写了一个演示the-super-tiny-compiler,请查看https://github.com/gengjiawen/the-super-tiny-compiler。
回答by Geng Jiawen
Just a small insight that might help you make your complex regular expression much simpler..
只是一个小小的见解,可能会帮助您使复杂的正则表达式更简单。
feel free to later apply any of the tips in answers above..
以后可以随意应用上述答案中的任何提示。
var text = ".................."; //assuming staring point
........
text = text
.replace(/\r/g,"##R##")
.replace(/\n/g,"##N##")
.replace(/\/\*(.*)\*\//g,"")
.replace(/##R##/g,"\r")
.replace(/##N##/g,"\n")
applying a little (independent) replacement of \rand \nwill simplify your regex A LOT!,
将一个小(独立)更换\r和\n将简化您的正则表达式了很多!,
originally even with
gandmmodifiers (global and "greedy" flags), you still won't succeed removing the comments (unless you custom-build a "character-walker" loop, or run the same reg-ex multiple times...) this is due some characteristics of the regular-expression matching left in limbo since ECMAScript 4 (ECMA-262)
最初即使使用
g和m修饰符(全局和“贪婪”标志),您仍然无法成功删除注释(除非您自定义构建“角色步行者”循环,或多次运行相同的正则表达式......)这是由于自 ECMAScript 4 ( ECMA-262)以来,正则表达式匹配的一些特征留在了边缘
What smart thing are doing here that is worth mentioning ?
这里有什么值得一提的明智之举?
This way we apply a nifty little trick known in Discrete mathematics(languages and grammar) as "replacement outside of our grammar", I'm using this unconventionally to "protect" the \rand \nareas in the text without actually applying too much computational-power to process them (as in cut/assemble etc..)
通过这种方式,我们将离散数学(语言和语法)中已知的一个漂亮的小技巧应用为“我们语法之外的替换”,我非常规地使用它来“保护”文本中的\r和\n区域,而没有实际应用太多的计算能力处理它们(如切割/组装等)
Here it's kind of a gamble since, essentially, ##R##and ##N##(although not so common), might be an existing phrase, but this is not an issue since the replacement can be infinitesimally-more complex.
这是一种赌博,因为从本质上讲,##R##并且##N##(虽然不那么常见)可能是一个现有的短语,但这不是问题,因为替换可能非常复杂。
In short,The regular-expressions will be simpler,
The regular-replacements will work as intended without that whitespace-bug..
And \nand \rwill be restored to their original placement, intact.
总之,正则表达式将会更简单,常规,替代将工作打算而没有空格的错误。而\n和\r将恢复到原来的位置,完整。

