Picture of Michael Lee
:wave: Hey hey, I'm Michael Lee and this is my site about being a developer, being a dad and making side projects.

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

Written on August 29, 2017

Using console.log() in JavaScript files are great to debug your code. But when it comes to shipping your code to production or a git repo it’s good to clean up your code by removing console.log()s.

If you’re using Atom as your text editor, it’s easy to do this using Regex.

  1. First pull up the find in buffer bar, by going to Find > Find in Buffer.
  2. In the first search field titled Find in current buffer type in console.log.*$
  3. Select the Use Regex option found in the upper right corner of the search panel that’s designated by the icon .*
  4. Press Find All to find all instances of console.log() and then press Replace All

"Find in Buffer panel"

In step 2, we used a regex to grab all instances of the string console.log till the end of the line. By using .*$ the . indicates we want to match any character, * is used to indicate we want to match in indefinite amount of any characters and then finally $ is used to indicate to match until the end of the line.

If you’re wondering why we left the second field of the Find in Buffer panel it’s because we don’t want to replace all the console.log()s with anything. By leaving it blank it is saying replace with nothing.

Thanks for taking the time to read this article. I'd love to stay in touch and share more tips on programming and design and side projects. Sign up and I'll send you my articles straight to your email.