Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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

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
Fabian Maass
Asked:
Fabian Maass
1 Solution
 
ozoCommented:
^[^/]*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
 
Fabian MaassAuthor Commented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now