如何在发布网站时删除js文件中的所有注释

JavaScript023

如何在发布网站时删除js文件中的所有注释,第1张

怎么用python过滤掉JS文件中的所有注释

txt中的注释有// 或者/**/,过滤代码如下:

BufferedReader reader = new BufferedReader(new FileReader("test.txt"))

PrintStream writer = new PrintStream(new FileOutputStream("test_new.txt"))

String buf

while ((buf=reader.readLine()) != null) {

   if (buf.isEmpty()) {continue}

   if (buf.matches("[/]+.*")) {

       buf = buf.replaceAll("[/]+(.*)", "$1") //去掉前面的/

   }

   buf = buf.replaceAll("\\s+(.*)", $1) //去掉前面的空格

可以用字符串替换函数replace1:str="1831,1829,1812,1829,134,171"str=str.replace("1812,","")至于去掉首尾,想到的方法就是先用“,”截取字符串到数组里,去掉首尾,然后再整合成字符串:var a=str.split(",")for(i=1i<a.length-1i++){b[i-1]=a[i]}c=b.join(",")2:使用字符串分割函数在聚合var str="hello world!"var items=str.split("o")会得到一个数组,数组中包括利用o分割后的多个字符串(不包括o)var newStr=items.join("")会得到一个新字符串,将数组中的数组使用空串连接成一个新字符串