{"id":721,"date":"2014-07-21T09:04:37","date_gmt":"2014-07-21T09:04:37","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=721"},"modified":"2014-07-26T11:26:18","modified_gmt":"2014-07-26T11:26:18","slug":"javascript-remove-comments-safely-and-correctly","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=721","title":{"rendered":"javascript remove comments safely and correctly"},"content":{"rendered":"<blockquote><p>Goal: remove the code comments correctly<\/p><\/blockquote>\n<p>One part of my javascript minimize process is to remove the comments from the code. As we know the there are two comment types in js:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n\/\/ single line comment\r\n\/*\r\n  multiline comment\r\n*\/\r\n<\/pre>\n<p>Here is a simple c# code to drop the comments.<\/p>\n<pre data-enlighter-language=\"csharp\" class=\"EnlighterJSRAW\">\r\n  string ret = Regex.Replace(code, @&quot;(\/\\*([^*]|[\\r\\n]|(\\*+([^*\/]|[\\r\\n])))*\\*+\/)|(\/\/.*)&quot;, string.Empty, RegexOptions.Multiline);\r\n<\/pre>\n<p>This code is seemingly correct but there are some special cases like the followings when it will fail:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n  var t = 'this is a simple comment: \/\/here is';\r\n  console.log(t);\r\n  \/* the result will be incorrect! *\/\r\n  var t = 'this is a simple comment: \r\n  console.log(t);\r\n<\/pre>\n<p>So this code contains a dangerous fake comment. I want to keep that ofcourse. My safe code is:<\/p>\n<pre data-enlighter-language=\"csharp\" class=\"EnlighterJSRAW\">\r\n  var re = @&quot;(@(?:&quot;&quot;[^&quot;&quot;]*&quot;&quot;)+|&quot;&quot;(?:[^&quot;&quot;\\n\\\\]+|\\\\.)*&quot;&quot;|'(?:[^'\\n\\\\]+|\\\\.)*')|\/\/.*|\/\\*(?s:.*?)\\*\/&quot;;\r\n  string ret = Regex.Replace(code, re, &quot;$1&quot;);\r\n<\/pre>\n<p>This code will keep everything inside marked strings <code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">&quot;.\/* fake comment *\/..&quot;, '. \/\/ fake comment ..'<\/code> and this is the expected behavior.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: remove the code comments correctly One part of my javascript minimize process is to remove the comments from the code. As we know the there are two comment types in js: Here is a simple c# code to drop the comments. This code is seemingly correct but there are some special cases like the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,3],"tags":[],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-net","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=721"}],"version-history":[{"count":3,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":726,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/721\/revisions\/726"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}