How to match a String with regular expressions, but don't match commented lines?

Posted on 2014-10-10
Medium Priority
Last Modified: 2014-10-10

i want to find javascript files, that contain the line "console.log" but only if this is uncommented. Like so:

this will match:
if (t.logEnabled() && typeof console != "undefined" && console.log) {

this won't match:

// if (typeof console != "undefined" && console.log) {
//            console.log(message);
       //      console.log("something else");

i thought this would do it, but apparently its not really correct:


I am aware that <!-- -> are also valid JavaScript comments, but i would put this aside for the moment.

Thank you for your help

Question by:Fabian Maass
LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 40372457

Although this would fail to match something like
  if( typeof console != "/" ){  console.log(message); }
if you are putting aside <!-- -> for the moment, I'm thinking you are also putting aside cases like this

Author Comment

by:Fabian Maass
ID: 40372487

thank you, i'll take ^[^//]*console\.log, this works perfectly fine.
(also im not sure why you think your case would fail, because the "/" is not at the start of the line / no comment)

closely after asking the question here, i realized i could do it as well in two steps (search for match, discard lines that start with //)

thanks for your help

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question