Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

As noted to do this properly you really need an HTML parser to extract the javascript and then a javascript parser to parse the javascript. A regex solution will never be 100% reliable BUT that said it is a great way to learn about regexes as you get to write a regex to do a task and then find out it does unexpected stuff!

perlre is the reference to regexes. Although s/this/that/ is the usual format ie using / to delimit the regex you can in fact use just about anything. When you have / symbols to match you either use a different delimiter of you have to escape the / symbols in the regex with a \ so \/ means match /. Compare the readability of:

$str =~ s/http:\/\///g; # and $str =~ s|http://||g; $str =~ s#http://##g; $str =~ s!http://!!g;

The first example is a bit harder to read due to the escapes. At least it is to me. Unfortunately the * char is a regex wildcard so you will still need to escape that. To get you started try:

$str =~ s!(/\*[^/]+\*/)!>>>> \1 <<<<!g; # /* comments */ $str =~ s!(\s//[^/\n]+)!>>>> \1 <<<<!g; # // comments print $str;

There will be plenty of edge cases not handled by this code but you can have a great learning experience trying to fix them without breaking other things! The () in the match section just allow you to explicity see what has happened as we capture the match and do some funky ascii highlighting in the replace section. To strip you just replace with nothing and don't need to capture but first you need to see what is happening. For debugging you may like to use:

$str =~ s!(fancy re here)! print "$1\n"; "" !ge;

That way when you run the code on a file you get the comments stripped but also printed to STDOUT so you can watch and make sure all the stuff that is going looks like comments!

Welcome to Perl, hope you have fun and it helps you to get the job done.


In reply to Re: Removing javascript comments by tachyon-II
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 cooling their heels in the Monastery: (6)
As of 2022-06-30 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?