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.

Stay in touch

Thanks for reading this article. I'd love to stay in touch and share more tips on programming and side projects with you. Sign up and I'll send you my articles straight to your email, you'll also get a free copy of the light themed version of my Git cheat sheet.
Git cheat sheet preview image