Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Removing javascript comments

by Joost (Canon)
on Apr 09, 2008 at 22:30 UTC ( #679364=note: print w/replies, xml ) Need Help??


in reply to Removing javascript comments

this is actually fairly complex to do correctly, since javascript has 2 different comment delimiters, and 'comment-like' constructs in string and regex literals (which are especially tricky to recognize) should not be mistaken for comments. You may want to use a full javascript grammar/parser instead of starting from scratch.

If you feel like experimenting, take a look at JE::Parser. Probably not for newbies, though...

Otherwise, you can take advantage of a few facts:

javascript string and regex literals are always single line. This means if you go trough the code line by line, ignoring string and regex literals, you can safely assume** any /* .. */ and // ... constructs left are comments (javascript does not allow for an empty regex literal). As I mentioned before, regex literals will be tricky, since the JS grammar for where regexes are valid (instead of divide operators) are fairly tricky, IOW, you will need to distinguish between

var res = a / 4 /* whatever */ ^^^^^^^^^^^^^^ comment
and
var res = /bla \/* .*/.exec("stuff"); ^^^^^^^ not a comment
for instance.

** in fact you can't, but it's /probably/ good enough.

update: fixed js code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://679364]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-01-25 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?