Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question Comment remover function

    In the following script I created a "removeComments()" function.

    It works, but it requires that the comment tags (// or /*)
    start in the first column of the text.

    Could anyone suggest a better function that allows the
    placement of comments ANYWHERE in the text similar
    to the way the JS interpreter treats its' comments?

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Comment Remover </title>
    <style type="text/css">
     #TArea { border:1px solid blue; height:15em; width:40em; }
     #PArea { border:1px solid blue; height:12em; width:40em; }
    </style>
    
    </head>
    <body>
    <textarea id="TArea">
    Sample text that may or may not
    contain single line comments
    // Single line comment
    
    or multi-line comments
    /* Multiple
      line
     comments 
    in text */
    
    Continued text
    
    Does not find // these type comments lines
    Nor does it remove /* these type comments */
    
    </textarea><br>
    <button onclick="document.getElementById('PArea').innerHTML=removeComments(document.getElementById('TArea').value)">
     Remove comment lines from above </button>
    <pre id="PArea"></pre>
    
    <script type="text/javascript">
    function removeComments(tstr) {  // remove any comment lines in the array
      var comment1 = comments = false;
      var rarr = tstr.split('\n');
      var tarr = [];
      for (var j=0; j<rarr.length; j++) {
        if (rarr[j].charAt(0) == '/') { 	  // check for comment line(s)
          if (rarr[j].charAt(1) == '/') { comment1 = true; } // found // comment line
          if (rarr[j].charAt(1) == '*') { comments = true; } // found start of /* comment line
        }
        if ((comment1) || (comments)) { comment1 = false; }  // some comment found
                                 else { tarr.push(rarr[j]); }
        if (rarr[j].indexOf('*/') != -1) { comments = false; }
      } //  tarr.pop();  // not sure if statement is needed or not
      return tarr.join('<br>');
    }
    </script>
    </body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Like this?
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Comment Remover </title>
    <style type="text/css">
     #TArea { border:1px solid blue; height:30em; width:40em; }
     #PArea { border:1px solid blue; height:30em; width:40em; }
    </style>
    
    </head>
    <body>
    <textarea id="TArea">
    Sample text that may or may not
    contain single line comments
    // Single line comment
    
    or multi-line comments
    /* Multiple
      line
     comments 
    in text */
    
    Continued text
    
    Does not find // these type comments lines
    Nor does it remove /* these type comments */
    
    And need to handle /* stuff
        like this */ properly.
    </textarea><br>
    <button onclick="document.getElementById('PArea').innerHTML=removeComments(document.getElementById('TArea').value)">
     Remove comment lines from above </button>
    <pre id="PArea"></pre>
    
    <script type="text/javascript">
    function removeComments(tstr) 
    {  
        var rarr = tstr.split('\n');
        var tarr = [];
        var j=0; 
        while ( j < rarr.length )
        {
            var line = rarr[j];
            var zap = line.indexOf("//");
            if ( zap >= 0 ) 
            {
                // don't push blank line!
                if ( zap > 0 ) tarr.push( line.substring(0,zap) );
            } else {
                zap = line.indexOf("/*");
                if ( zap >= 0 ) 
                {
                    var zoop = line.indexOf("*/");
                    if ( zoop > 0 )
                    {
                        // just remove the part of the line between /* and */
                        line = line.replace( /\/\*.*?\*\//, "" );
                        if ( line.length > 0 ) tarr.push( line );
                    } else { 
                        // must be multiline comment
                        // push the part before the / *
                        if ( zap > 0 ) tarr.push( line.substring(0,zap) );
                        // then search for matching * /
                        while ( j < rarr.length )
                        {
                            ++j;
                            line = rarr[j];
                            zoop = line.indexOf("*/");
                            if ( zoop >= 0 )
                            {
                                line = line.substring(zoop+2);
                                if ( line.length > 0 ) tarr.push(line);
                                break; // out of this inner loop
                            }
                        }
                    } // end of multiline comment
                } else {  // end of / * style comment
                    tarr.push(line);
                }
            } // end of else-is-not-//-style commment
            ++j;
        }
        return tarr.join('<br>');
    }
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Or a little shorter

    Code:
    function removeComments (code) {
        return code
            .replace( /\/\/[^\n]*/g, '' )
            .replace( /\/\*[\s\S]*?\*\//g, '' );
    }
    Like this, it only removes the content of the line, not the line itself (i.e. it leaves blank lines). That can be done, too, with slight adjustments, though.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    DOH on me. Why not just use a regular expression to zap all // comments as well as any /*...*/ contained on a single line?

    That's what this code does, so it's more compact. And seems to get better output.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Comment Remover </title>
    <style type="text/css">
     #TArea { border:1px solid blue; height:30em; width:40em; }
     #PArea { border:1px solid blue; height:30em; width:40em; }
    </style>
    
    </head>
    <body>
    <textarea id="TArea">
    Sample text that may or may not
    contain single line comments
    // Single line comment
    
    or multi-line comments
    /* Multiple
      line
     comments 
    in text */
    
    Continued text
    
    Does not find // these type comments lines
    Nor does it remove /* these type comments */
    
    And need to handle /* stuff
        like this */ properly.
    </textarea><br>
    <button onclick="document.getElementById('PArea').innerHTML=removeComments(document.getElementById('TArea').value)">
     Remove comment lines from above </button>
    <pre id="PArea"></pre>
    
    <script type="text/javascript">
    function removeComments(tstr) 
    {  
        var rarr = tstr.split('\n');
        var tarr = [];
        var j=0; 
        while ( j < rarr.length )
        {
            // get line after removing /*xxx*/ comments or //xxxx comments:
            var line = rarr[j].replace(/(\/\*.*?\*\/|\/\/.*?$)/,"");
            // if line is blank after that, just ignore it...
            // but if not, then:
            if ( line.length > 0 )
            {       
                var zap = line.indexOf("/*");
                // is there the start of a multiline comment in there, still?
                if ( zap < 0 ) 
                {
                    // nope...so the iine should appear in the result
                    tarr.push(line);
                } else {
                    // multiline comment and we just found the /* start
                    // so search for matching * /
                    while ( j < rarr.length )
                    {
                        ++j;
                        line = rarr[j];
                        var zoop = line.indexOf("*/");
                        if ( zoop >= 0 )
                        {
                            // found the */
                            line = line.substring(zoop+2); // kill stuff before the */
                            if ( line.length > 0 ) tarr.push(line); // only push if anything left
                            break; // out of this inner loop
                        }
                    }
                }
            } 
            ++j;
        }
        return tarr.join('<br>');
    }
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Quote Originally Posted by Airblader View Post
    Or a little shorter

    Code:
    function removeComments (code) {
        return code
            .replace( /\/\/[^\n]*/g, '' )
            .replace( /\/\*[\s\S]*?\*\//g, '' );
    }
    Like this, it only removes the content of the line, not the line itself (i.e. it leaves blank lines). That can be done, too, with slight adjustments, though.
    LOL! SHEESH! I got caught up in looking at his code and didn't think about doing the whole schmear at once.

    Can't you then get rid of the blank lines with simply:
    Code:
    function removeComments (code) {
        return code
            .replace( /\/\/[^\n]*/g, '' )
            .replace( /\/\*[\s\S]*?\*\//g, '' )
            .replace( /\n{2,}/gm, '\n' );
    }
    Hmmm...no, I guess not. That would get rid of blank lines that were there in the original text, which presumably would be a no-no.

    Well, if getting rid of all blank lines is okay, that certainly works and is easy.
    Last edited by Old Pedant; 07-13-2013 at 01:49 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Old Pedant View Post
    Why not just use a regular expression to zap all // comments as well as any /*...*/ contained on a single line?
    Probably more a rhetorical question, but the reason I did it this way: I find it easier to understand. All the escaping makes the expression hard enough to read already. Though advanced developers surely will understand (and maybe prefer) the one-liner.

    How about this?

    Code:
    function removeComments (code) {
        return code
            .replace( /(^|\n).*?[^\s].*?\/\/[^\n]*/g, '' )
            .replace( /(^|\n)\s*\/\/[^\n]*\n/g, '$1' )
            .replace( /\/\*[\s\S]*?\*\/([^\S\n]*\n)?/g, '' );
    }
    Edit: I changed it a little bit to fix a little flaw.
    Last edited by Airblader; 07-13-2013 at 02:02 AM.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Quote Originally Posted by Airblader View Post
    Originally Posted by Old Pedant
    Why not just use a regular expression to zap all // comments as well as any /*...*/ contained on a single line?
    Ummm...I was asking myself that question. I hadn't seen your post yet. So definitely rhetorical.

    I really *MUCH* prefer the way you wrote that, putting one .replace per code line. Neat and clean and readable. Kudos.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...I was asking myself that question. I hadn't seen your post yet. So definitely rhetorical.
    Oh, that makes sense. Forget what I said then.

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Man, I didn't expect so many ways that I could not think of!!!

    Thanks to all for the code.



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •