DEV Community

Discussion on: Remove all console.log()s using Regex in Atom

Collapse
 
palle profile image
Palle • Edited

Parsing a context free language like JavaScript with regular expressions can lead to unpredictable side effects, as regular expressions can only match words of regular languages correctly.

If somebody wrote a comment like // lorem ipsum console.log dolor sit amet, the regex would also match dolor sit amet and it would be deleted. And even if the regex was fixed to eliminate this problem by matching the closing parenthesis (console\.log\(.*\);?) this could also not work in some cases.

As suggested by other users, using a logging framework, which can be disabled, may be the better solution. Or use a debugger if possible and reduce the number of console.log statements. If none of this helps, maybe there is a refactoring tool that can safely delete all console.logs.