Philipp Kyeck
Philipp Kyeck

Reputation: 18830

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

terminal screenshot

Upvotes: 8

Views: 7849

Answers (9)

Trinadh Koya
Trinadh Koya

Reputation: 1087

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.

Upvotes: 0

mathewguest
mathewguest

Reputation: 650

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

Upvotes: 0

Ecko123
Ecko123

Reputation: 308

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

Upvotes: 1

Keegan 82
Keegan 82

Reputation: 404

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.

Upvotes: 0

rodrigocorsi
rodrigocorsi

Reputation: 33

I try this:

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

See the test: Regex Tester

Upvotes: 1

Ben
Ben

Reputation: 2441

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

Upvotes: 0

windylcx
windylcx

Reputation: 21

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"

Upvotes: 1

Martin
Martin

Reputation: 38309

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

Upvotes: 12

Zsolt Botykai
Zsolt Botykai

Reputation: 51653

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

Upvotes: 2

Related Questions