开发者

Using sed to remove all console.log from javascript file

I'm trying to remove all my console.log, console.dir etc. from my JS file before minifying it with YUI (on osx).

The regex I got for the console statements looks like this:

console.(log|debug|info|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\((.*)\);?

and it works if I test it with the RegExr. But it won't work with sed.

What do I have to change to get this working?

sed 's/___???___//g' <$RESULT >$RESULT_STRIPPED

开发者_开发知识库 update

After getting the first answer I tried

sed 's/console.log(.*)\;//g' <test.js >result.js

and this works, but when I add an OR

sed 's/console.\(log\|dir\)(.*)\;//g' <test.js >result.js

it doesn't replace the "logs":

Using sed to remove all console.log from javascript file


Your original expression looks fine. You just need to pass the -E flag to sed, for extended regular expressions:

sed -E 's/console.(log|debug|info|...|count)\((.*)\);?//g'

The difference between these types of regular expressions is explained in man re_format. To be honest I have never read that page, but instead simply tack on an -E when things don't work as expected. =)


You must escape ( (for grouping) and | (for oring) in sed's regex syntax. E.g.:

sed 's/console.\(log\|debug\|info\|warn\|error\|assert\|dir\|dirxml\|trace\|group\|groupEnd\|time\|timeEnd\|profile\|profileEnd\|count\)(.*);\?//g'

UPDATE example:

$ sed 's/console.\(log\|debug\|info\|warn\|error\|assert\|dir\|dirxml\|trace\|group\|groupEnd\|time\|timeEnd\|profile\|profileEnd\|count\)(.*);\?//g'
console.log # <- input line, not matches, no replacement printed on next line
console.log
console.log() # <- input line, matches, no printing

console.log(blabla); # <- input line, matches, no printing

console.log(blabla) # <- input line, matches, no printing

console.debug();  # <- input line, matches, no printing

console.debug(BAZINGA)  # <- input line, matches, no printing

DATA console.info(ditto); DATA2 # <- input line, matches, printing of expected data
DATA  DATA2

HTH


I also find the way to remove all the console.log , and i am trying to use python to do this, but i find the Regex is not work for. my writing like this:

var re=/^console.log(.*);?$/;

but it will match the following string:

'console.log(23);alert(234dsf);'

does it work? with the

"s/console.(log|debug|info|...|count)((.*));?//g"


I try this:

sed -E 's/console.(log|debug|info)( ?| +)\([^;]*\);//g'

See the test: Regex Tester


Here's my implementation

for i in $(find ./dir -name "*.js")
do

sed -E 's/console\.(log|warn|error|assert..timeEnd)\((.*)\);?//g' $i > ${i}.copy && mv ${i}.copy $i

done

took the sed thing from github


I was feeling lazy and hoping to find a script to copy & paste. Alas there wasn't one, so for the lazy like me, here is mine. It goes in a file named something like 'minify.sh' in the same directory as the files to minify. It will overwrite the original file and it needs to be executable.

#!/bin/bash

for f in *.js
do
    sed -Ei 's/console.(log|debug|info)\((.*)\);?//g' $f
    yui-compressor $f -o $f
done


I'd just like to add here that I was running into issues with namespaced console.logs such as window.console.log. Also Tweenmax.js has some interesting uses of console.log in some parts such as

       window.console&&console.log(t)

So I used this

  sed -i.bak s/[^\&a-zA-Z0-9\.]console.log\(/\\/\\//g js/combined.js

The regex effectively says replace all console.logs that don't start with &, alphanumerics, and . with a '//' comment, which uglify later takes out.


Rodrigocorsi's works with nested parentheses. I added a ? after the ; because yuicompressor was omitting some semicolons.


It is probable that the reason this is not working is that you are not 'limiting' the regex to not include a closing parenthesises ()) in the method parameters.

Try this regular expression:

console\.(log|trace|error)\(([^)]+)\);

Remember to include the rest of your method names in the capture group.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜