Javascript singleline comments removed with bash

Have tried to get this to work for a while now without any success. I know there are similar posts about this topic but their examples doesn’t work or their regexp’s are to violent for my purpouse.
This is to heavy:
Comprehensive RegExp to remove JavaScript comments

My issue is quite simple, i have a unified javascript file created with a deploy.sh script and would like to have every line removed where the first characters occuring are “//” ignoring any preceding whitespaces and tabs like this:

// Remove this line
var foobar = "sometext";
    // remove this line with tab(s) before comment
    function foobar(); // Do not remove this!
  // remove this line with whitespace(s) before comment

This should be achived with a linux/ubuntu native tool or program language executed inside a shellscript without any external files useing something like sed.

I think having a convention of just using theese conditions for comment lines as removable should be quite straightforward with a minimal risk of misinterpreting. If i then like to have some comments to show in the production version i can then use the multiline comment annotation /* Comment */


Source: linux

Leave a Reply