8

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":

terminal screenshot

Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123

9 Answers9

12

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. =)

Martin
  • 37,119
  • 15
  • 73
  • 82
  • 3
    Your my best friend now! ```console.(log|debug|info|count)\((.*)\);?``` in sublime safely cleaned 2k lines. no more ugly logs :-) – James Harrington Sep 26 '14 at 18:23
2

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

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • What you had tried as a second solution is searching for `console.` then any of `log` OR `dir` then a pair of `()` with our without any character within (e.g. `()` and `(((((()` are valid matches) then a `;`. – Zsolt Botykai Jul 06 '11 at 12:28
  • You should have had added that you are on OSX earlier... OSX sed != GNU sed. See the answer by Martin. – Zsolt Botykai Jul 06 '11 at 17:51
  • sorry, for that ... didn't know about the differences + added the osx part with the first update – Philipp Kyeck Jul 07 '11 at 08:53
1

I try this:

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

See the test: Regex Tester

1

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

Ecko123
  • 308
  • 1
  • 4
  • 16
1

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"

windylcx
  • 21
  • 1
  • 7
0

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
Ben
  • 2,143
  • 2
  • 19
  • 27
0

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.

Keegan 82
  • 394
  • 2
  • 11
0

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

mathewguest
  • 630
  • 5
  • 10
0

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.

Michael Coxon
  • 5,311
  • 1
  • 24
  • 51
Trinadh Koya
  • 1,089
  • 15
  • 19