Javascript评论stripper [英] Javascript comment stripper

查看:157
本文介绍了Javascript评论stripper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一些工具来从Javascript源删除cooments。我能够谷歌一些,但没有一个满足以下要求:一切都应该保留,因为它是,特别是白色空间不被删除,但如果一个评论采取整行,该行也被删除



很快,我想能够从带有注释的格式很好的源转换为同样格式的源,没有注释。仅删除包含注释的行,并删除跟踪注释以及尾随空格。



你知道这样工作的任何工具吗?



strong> EDIT :我尝试更具体。使用正则表达式是不可能的,因为字符 // / * 也可能出现在字符串,

该工具应接受此输入

  var a = true; 

//下面的代码是有用的
var b = 2; //真的很有用
/ **
永远不要这样做
var c = 3;
* /
var d = 4;

并提供此输出

  var a = true; 

var b = 2;
var d = 4;


解决方案

这里是一些代码:
请查看:此处



此外,这里是我的代码示例,您可以立即测试网页 p>

这里有一个我没有写的,可以方便,虽然他的代码将失败某些正则表达式文字: http://james.padolsey.com/javascript/removing-comments-in-javascript/



编辑:
我写的代码是一样的。我不更新它。如果有错误,您可以修复它。


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.

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;

解决方案

Here's some code I whipped up: Check it out: here

Also here is 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/

EDIT: The code I wrote is as is. I am not updating it. If there is a bug, you can fix it.

这篇关于Javascript评论stripper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆