Comments on: JavaScript comment removal – revisited https://j11y.io/snippets/javascript-comment-removal-revisted/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: caii https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1312 Fri, 02 Apr 2010 07:11:15 +0000 https://j11y.io/?p=1176#comment-1312 “the comment problem can’t be solved with regular expressions. ”

Maybe can be solved with regular expressions:

function removeComments(str){
return str.replace(reg,function(n,Arg_comments){
return Acomments?”:n
})
/*
reg=/(comments)|(string)|(regexp)/g
when Arg_comments is not empty string , it’s mean comments is found ,replace it with “”, if not , return the string (like string “‘/***/'” or “//..” or /./**/ ….) itself .
*/
}

]]>
By: Zoltan Hawryluk https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1311 Fri, 05 Mar 2010 14:56:56 +0000 https://j11y.io/?p=1176#comment-1311 Hello there.

Thanks for the post. You saved me a lot of time – I needed something like this to parse the comments out a CSS style sheet. Excellent research!

]]>
By: James Rourke https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1310 Sun, 15 Nov 2009 23:42:19 +0000 https://j11y.io/?p=1176#comment-1310 Incidentally, you’ve misspelled “primitives”. 😉

]]>
By: Alberto Lepe https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1309 Wed, 04 Nov 2009 05:28:59 +0000 https://j11y.io/?p=1176#comment-1309 I searched in many places but none of those codes worked for me. I wanted to remove starting multiline comments, and your regex worked without problem! (I will give you creds inside my code). Thank you!

]]>
By: Jason P https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1308 Fri, 23 Oct 2009 00:56:54 +0000 https://j11y.io/?p=1176#comment-1308 try this one to capture both SingleLine and MultiLine Comments:

/(/*[u0000-uFFFF]*?(?=*/)*/|//[^u000A|u000D|u2028|u2029]*)/

Works for all of the examples in the comments

I am sure that [u0000-uFFFF] is probably too broad but I could not find a good example of what a unicode “code point” was according to ecmascript spec.

]]>
By: tomh https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1307 Thu, 15 Oct 2009 10:02:21 +0000 https://j11y.io/?p=1176#comment-1307 // comment
/* comment */ program //comment

Still no perferct, sorry.
This source doesn’t print “program” as expected, but “/comment”.
But nice try!

]]>
By: James https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1306 Sat, 12 Sep 2009 18:37:51 +0000 https://j11y.io/?p=1176#comment-1306 Just been testing it a little more and have found some more bugs. I guess it’s true what they say; the comment problem can’t be solved with regular expressions.

@Piotr, any minifying program worth its salt won’t screw up that line.

]]>
By: Piotr Wasilewski https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1305 Sat, 12 Sep 2009 11:44:55 +0000 https://j11y.io/?p=1176#comment-1305 Hi,

I’ve noticed that you often use this expression: ‘some_string’ + +new Date().
It’s good practice to add brackets ‘some_string’ + (+new Date()) to avoid errors after minifying code.

]]>
By: James https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1304 Sat, 12 Sep 2009 07:49:46 +0000 https://j11y.io/?p=1176#comment-1304 @Corey, Ah.. thanks. Fixed now! 🙂

@Nicolaj, fixed too, (forgot to add $ to regex)

]]>
By: Nicolaj Kirkgaard Nielsen https://j11y.io/snippets/javascript-comment-removal-revisted/#comment-1303 Sat, 12 Sep 2009 06:01:52 +0000 https://j11y.io/?p=1176#comment-1303 Hi James…

Nice script. Lord knows I couldn’t have come up with anything in that league.

It seems that if the last line is a single line comment and the line doesn’t have a break at the end, the script doesn’t recognize the line as a comment.

var ok = "not really"; 
// This is a comment
// Shouldn't this be a comment too?

Doesn’t recognize the last line…

]]>