Skip to main content
gokul1242
Inspiring
April 19, 2014
Answered

Excluding CF and Js Commenting tags

  • April 19, 2014
  • 3 replies
  • 581 views

I am counting the number of lines on code in .cfm pages. But i want to exclude the lines between the CF and Javascript commenting tags while doing this.

I tried replacing the lines in between the commenting tags with empty string like the below one.

For CF commenting tags :  <cfset filecontent=rereplacenocase(filecontent,"<![^<]*>","","all")>

For Js Commenting tags  :  <cfset filecontent=rereplacenocase(filecontent,"/*[^<]*/","","all")>

But mostly the above method does not exclude the codes. can anyone suggest any alternate method to achieve this.

This topic has been closed for replies.
Correct answer gokul1242

The below code worked for me.

For CF Comments : <cfset filecontent = rereplace(filecontent, "<!---.*?--->", "", "all")>

For .js comments:   <cfset filecontent = rereplace(filecontent, "\/\*.*?\*\/", "", "all")>

3 replies

BKBK
Community Expert
Community Expert
April 22, 2014

gokul1242 wrote:

I am counting the number of lines on code in .cfm pages. But i want to exclude the lines between the CF and Javascript commenting tags while doing this.

What do you need the number of lines of code for? You may not need to exclude the comments.

Comments, if well motivated, are an integral part of the code! In fact, in most software engineering and project management analyses, the required value of the number of Source Lines of Code (SLOC) actually includes comment lines!

gokul1242
gokul1242AuthorCorrect answer
Inspiring
April 22, 2014

The below code worked for me.

For CF Comments : <cfset filecontent = rereplace(filecontent, "<!---.*?--->", "", "all")>

For .js comments:   <cfset filecontent = rereplace(filecontent, "\/\*.*?\*\/", "", "all")>

cherdt
Inspiring
April 21, 2014

Did you mean:

For CF commenting tags :  <cfset filecontent=rereplacenocase(filecontent,"<![^>]*>","","all")>

Javascript /* */ are trickier, since both * and / are valid symbols within comments. This should work:

<cfset filecontent = REReplace(filecontent, "/\*.*\*/", "", "ALL")>

Don't forget the "//" style Javascript comments, which you can probably match with something like:

<cfset filecontent = REReplace(filecontent, "//.*(\r\n|\n)", "", "ALL")>

(It is seldom a good idea to use .* in regular expressions--you often get more than you bargained for. But these examples are just for illustration.)