Regular expression to remove comments JS, HTML and CSS

1

I need a regular expression that works to remove comments from HTML, JS and CSS.

The biggest problem I find is in the line comments. As in the project where I work there is a lot of JS to fire in HTML, if I try to detect // until end of line, also matchea urls.

Even so, I solved it with this:

/((?<!(\:))(\/\/)(.|\s)*?(\n))/g

But it's not enough, since there can be a type% ur_of% and it's loaded too.

With this I solved it:

/(?<=\")((?<!(\:))(\/\/)(.|\s)*?(\n))(?=\")/g

But now I am faced with the problem that sometimes (for reasons I do not know) there may be something in front of those two bars. Ex: src="//jquery.etc"

How can I directly detect if it's encapsulated so it does not get deleted?

Thanks in advance.

    
asked by Victor Vkr 08.08.2018 в 11:56
source

2 answers

0

You should try to find the relevant tags first:

In Javascript (text that you find between <script> and </script> ) you will find comments of two types:

//esto es un comentario de una línea
/*Esto es un comentario en bloque, puede tener o no
  varias líneas*/

If I'm not wrong, in a block of CSS code ( <style> ...</style> ) you can find comments like this:

h1 {
  /*color: red; comentado*/
  margin-top: 5px;
}

But comments are not allowed with // , that produces a syntax error.

And finally, HTML uses the following to comment code:

<p> Esto es un párrafo <!-- Esto es un comentario --> </p>

There is an exception, which is when you put styles as attributes, since it is CSS code, but it is not usual to see them:

<p style="color: red; /*el color es rojo*/"> ...</p>

So I would try the following: Search first the labels that define the start and end of each language and then search for the corresponding comments and delete them

    
answered by 08.08.2018 в 12:28
0

You could try with these regular expressions:

<?php
// Eliminar comentarios html
$txt = preg_replace('/\h*<!--.*?-->\h*/s', '', $txt);

// Eliminar comentarios /* */
$txt = preg_replace('/\h*\/\*.*?\*\/\h*/s', '', $txt);

// Eliminar comentarios //
$txt = preg_replace('/^\h*(?|(.*"[^"]*\/\/[^"]*".*)|(.*)\/\/.*\h*)$/m', '$1', $txt);

Here you have a working example

    
answered by 08.08.2018 в 13:45