Solved

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

Posted on 2014-10-10
2
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

622 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