DEV Community

Discussion on: Lookaheads in Javascript

Collapse
gdebenito profile image
Gonzalo • Edited on

Hey! Thanks to you I made something!! A regex to detect uncommented console.log()!! What do you think?

const regEx = /^(?!.*\/\/)(?=.*console\.log)/g
const examples = [
    '  // console.log("asdf")',
    '  //        console.log("asdf")',
    '  //        console.log("asdf")',
    '       console.log("asdf")   ',
    '       console.log(',
]

examples.forEach(
    (example) => {
        console.log(
            example,
            example.match(regEx) !== null
        )
    }
)

And the output:

  // console.log("asdf") false
  //        console.log("asdf") false
  //        console.log("asdf") false
       console.log("asdf")    true
       console.log( true
Collapse
savagepixie profile image
SavagePixie Author

That's pretty cool! I imagine something like this would be useful to comment console.logs once you're done debugging.

Collapse
moopet profile image
Ben Sinclair

Your next goal is to get it to work with these :)

console.log("foo"); // console.log("bar");

/*****
* console.log("baz");
*****/

console.table(['foo', 'bar']);