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
Solved

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

Posted on 2014-10-10
2
197 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Basic Regex for use in webservice 1 67
Problem of RegEx to match the first occurence of 10 56
Need help decoding the following expressions 1 48
REReplaceNoCase help 1 43
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…

828 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