Note that there are some explanatory texts on larger screens.

plurals
  1. POREGEX: How to remove comments from javascripts, using PHP code
    primarykey
    data
    text
    <p>I am combining all my javascriupt into one neat file in order to lower http requests! Im stuck removing the comments <code>/* comments */</code> and <code>// comments</code>. My level is by far below minification or parsing stuff. I know how to make macaroni strings. Anything more complex than that, you will not find in my computer or kitchen, SO:</p> <h1>QUESTION</h1> <p>meanwhile at combining it to one file, i want to remove all comments.<br> <strong>What is the correct regex for this?</strong></p> <pre><code>&lt;?php header('Content-type: text/javascript'); $offset = 60 * 60 * 24; // Cache for a day header ('Cache-Control: max-age=' . $offset . ', must-revalidate'); header ('Expires: ' . gmdate ("D, d M Y H:i:s", time() + $offset) . ' GMT'); ob_start("compress"); function compress($buffer) { # NOT SURE, not all new lines are removed?? # remove tabs, spaces, newlines, etc. $buffer = str_replace(array("\r\n", "\r", "\t", ' ', ' '), '', $buffer); # WORKS !!! # remove comments / XXXXXXX $buffer = preg_replace('(// .+)', '', $buffer); ######################################################################## # !! STUCK HERE !! OUTPUT FILE LOOKS OK BUT WEBSITE DOESNT LOAD OK IF THIS IS ON # remove comments / * XXX (enters etc) XXXX * / # $buffer = preg_replace('#/\*(?:.(?!/)|[^\*](?=/)|(?&lt;!\*)/)*\*/#s', '', $buffer); ######################################################################## return $buffer; } include('../file1.js'); include('../file2.js'); ob_end_flush(); ?&gt; </code></pre> <p>It would be great if it would catch and delete the following:</p> <pre><code>/* XXXX */ /* XXXX XXXX */ </code></pre> <p>Thats all! Cant get it to work nomatter what regex i use even with this incredible tool, where i FOUND the right match to be:</p> <pre><code>RegExp: /\/\*(?:.(?!/)|[^\*](?=/)|(?&lt;!\*)/)*\*\//gs pattern: \/\*(?:.(?!/)|[^\*](?=/)|(?&lt;!\*)/)*\*\/ flags: gs </code></pre> <p><a href="http://gskinner.com/RegExr/" rel="nofollow">http://gskinner.com/RegExr/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload