0

I have searched and tried methods mentioned in many questions but all methods are taking in consideration only simple use cases. My question is to remove console.log('some data') for all below cases.

I need a bash script regex to remove below console.log() and keep the rest of the content intact.

console.log('Some text', variable);

console.log('some text with (a) bracket');

function myfuncton(console.log('some text with covering bracket')); to function myfuncton();

Note: In the last example, all the content is in single line without any spaces.

I am trying with below regex but it removes all the brackets preceding closing bracket of console.log. I know the reason but couldn't figure out the regex.

SEARCH_STRING="(\/\/)*console.(log|info|warn)\((.*)\)?"

Do I need to use some other method or it can be done using regex.

Deepak jha
  • 298
  • 5
  • 19

2 Answers2

0

Regular expressions are a poor tool for this job.

You want to use an abstract syntax tree.

https://github.com/sindresorhus/strip-debug

Seth Holladay
  • 8,951
  • 3
  • 34
  • 43
0

If all your console.log occurrences are in the form described in the question, you could simply use a regex like console\.(?:log|info|warn)\('[^']*'(?:,[^)]*)?\);?, but Seth Holladay's approach is indeed safer.

Johannes Riecken
  • 2,301
  • 16
  • 17