Skip to main content

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

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.

Wrapping up, the regex to remove is the following:

console.log.*$
Sebastián Becerra

Trabajo con tecnologías web en arquitecturas Cloud. Me gusta descubrir y aprender nuevas tecnologías en Layouts (CSS, Sass, Less), Frameworks de Desarrollo (JQuery, AngularJS, Ionic) y plataformas Web (Wordpress, Joomla, Prestashop). He colaborado con diferentes proyectos de software libre y código abierto con fines sociales. Además participé en distintos eventos TI exponiendo sobre comunidades y educando sobre tecnologías Web.

Leave a Reply