Comments on: Removing comments in JavaScript https://j11y.io/javascript/removing-comments-in-javascript/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: JavaScript comment removal – revisted – James Padolsey https://j11y.io/javascript/removing-comments-in-javascript/#comment-943 Fri, 11 Sep 2009 19:25:40 +0000 https://j11y.io/?p=835#comment-943 […] while ago I posted a method I had been using at the time to remove comments from JavaScript code. It was pretty decent – […]

]]>
By: adam https://j11y.io/javascript/removing-comments-in-javascript/#comment-942 Thu, 23 Jul 2009 20:29:47 +0000 https://j11y.io/?p=835#comment-942 great thanks for that piece of code šŸ˜€ it helped me very much!

]]>
By: James https://j11y.io/javascript/removing-comments-in-javascript/#comment-940 Mon, 25 May 2009 17:02:32 +0000 https://j11y.io/?p=835#comment-940 Thanks for the comments!

@Lea, I thought about whether or not it’d be achievable using regex lookaheads/behinds but I figured, even if JavaScript supported lookbehinds, it would be quite slow.

@Rick, you right; it would be tripped up by that, but it’s not valid JavaScript so I’m not bothered. "\"" would throw a syntax error (since the first is escaping the second there is nothing left to escape the second ").

@Vasco, It seems so; I read more about this technique over here: http://www.codeproject.com/KB/cs/jscompress.aspx – they seem to be using a similar method to remove comments. It’s funny how something that seems so simple can end up being quite complicated…

]]>
By: Vasco Fernandes https://j11y.io/javascript/removing-comments-in-javascript/#comment-939 Mon, 25 May 2009 13:06:30 +0000 https://j11y.io/?p=835#comment-939 Actually, one of the first things you’ll learn in a compilers class is that no regular expression can take care of the comments problem, you need a full blown parser to do that. So i donĀ“t think you can do it any simpler way than going through every character, you can however, if you’re willing to do it in c/c++ or java, use something like lex to do it with only a few lines of code.

]]>
By: Rick https://j11y.io/javascript/removing-comments-in-javascript/#comment-938 Mon, 25 May 2009 10:14:28 +0000 https://j11y.io/?p=835#comment-938 This looks very handy indeed. Very useful for CSS and Javascript optimisers among other things.

I may be wrong, but it looks like it could still be tripped up with double backslashes… Eg. “a\”” /*Boo!*/

]]>
By: Lea Verou https://j11y.io/javascript/removing-comments-in-javascript/#comment-937 Mon, 25 May 2009 00:23:16 +0000 https://j11y.io/?p=835#comment-937 I think you’ll come across similar problems when writing any kind of parser (and your script is actually a parser for JavaScript comments).

I’ve stumbled across it several times in the past, the last two while writing a syntax highlighter and the latest while writing a small parser for google-style search queries. In the first case (the syntax highlighter), I decided it’s not worth the extra resources for such edge cases, since it was mainly for my personal use and I wanted something fast, even if I had to sacrifice 100% correctness.

In the second, more recent case, I decided to take a similar approach as you did, since such cases were going to be really common and it’s also a commercial project, so I can’t have search queries failing due to lazy parsing.

I’m also really interested if there’s a better solution. I think perhaps it would be somehow possible by combining regex lookahead and lookbehind (in languages that support them, JS doesn’t support lookbehind šŸ™ ) but I’m not very experienced with those two.

]]>