Solved

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

Posted on 2014-10-10
2
188 Views
Last Modified: 2014-10-10
Hi,

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) {
                  console.log(obj);
}

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:

[^\/\/].*console\.log.*

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

Thank you for your help

fabian
0
Comment
Question by:Fabian Maass
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40372457
^[^/]*console\.log

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
0
 

Author Comment

by:Fabian Maass
ID: 40372487
hi,

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
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now