Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Removing javascript comments by Joost
in thread Removing javascript comments by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2022-01-29 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?