1

To clarify first, I already have a compression tool that successfully compresses EVERYTHING else so I don't need a long complicated preg_replace regex, just a simple preg_replace or str_replace rule will do.

I ONLY want to remove JavaScript comments that start with "// " (including the space so I don't erase URLs) up until the new line. THAT'S IT! Once again, I don't need to replace /* */ or any other variations as I have a script that does that already; the only thing it's missing is this one feature so I want to clean up the input with this first, then hand it over to the script.

Here are screenshots with examples of comments I would like to remove:

Screenshot 1 Screenshot 2

I would really appreciate help with this! :-) THANKS!

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
Kevin
  • 19
  • 2
  • 1
    http://meta.stackoverflow.com/q/303812/4227915 –  Sep 08 '15 at 16:35
  • 2
    Using PHP or JavaScript? – Teemu Sep 08 '15 at 16:39
  • http://stackoverflow.com/questions/5989315/regex-for-match-replacing-javascript-comments-both-multiline-and-inline but it seems easier to use one that is already written... And hopefully you have a test with a string that has a url to make sure you do not remove it. – epascarello Sep 08 '15 at 16:39
  • 1
    It isn't a trivial task to do that with regex, in particular because `//` can be inside a string. – Casimir et Hippolyte Sep 08 '15 at 17:27
  • You can try https://regex101.com/r/cQ0dL8/1 –  Sep 08 '15 at 18:24
  • I don't want to replace the text with a blank, I want to REMOVE THE ENTIRE LINE. hope that helps. – Kevin Sep 08 '15 at 19:05
  • So, try this: _https://regex101.com/r/cQ0dL8/2_ .. The idea is the same –  Sep 08 '15 at 19:36
  • hey washington guedes how do I convert that into a preg_replace or str_replace command? – Kevin Sep 08 '15 at 20:05
  • Do it like @PedroPinheiro did in his answer.. just change this line `$re = "[^\n]*\/\/[^\n]*";` –  Sep 09 '15 at 13:24

2 Answers2

1

Try this code:

$re = "~\\n? */{2,} .*~"; 
$str = "text\n\n/*\ntext\n*\n    //example1\n    //example2\n\ntext\n\n//   example 3"; 
$subst = ""; 

$result = preg_replace($re, $subst, $str);

The regex matches two or more / following by a space like you asked in the question. When you replace it will erase the whole line or until the spaces and line breaks that were

Demo

Pedro Pinheiro
  • 1,059
  • 14
  • 32
  • If you use a delimiter other than / for your expression, you won't have to escape the delimiter, such as `'~// .*$~'` will match slash slash space followed by anything to the end of the line. – kainaw Sep 08 '15 at 17:09
  • YIKES! I tried this suggestion and it deletes almost everything leaving just the following in the html: – Kevin Sep 08 '15 at 18:11
  • I don't want to replace the text with a blank, I want to REMOVE THE ENTIRE LINE. hope that helps... – Kevin Sep 08 '15 at 19:06
  • @Kevin, sorry about that, I forgot about the URLs. I updated the answer now with the correct regex. – Pedro Pinheiro Sep 08 '15 at 19:54
  • hey pedro, okay going to try it and let you know – Kevin Sep 08 '15 at 20:12
  • hmm, you and the other guy have slightly different results. can you tell me what's the difference between: THIS ~\\n? */{2,} .*~ AND THIS /^\h*\/\/.*$/m – Kevin Sep 08 '15 at 22:21
1

I'd use a regex with the multi-lined modifier, m,

/^\h*\/\/.*$/m

This will find any lines that start with a // regardless of the whitespace preceding it. Anything after the first // will be removed until the end of that line.

Regex101 Demo: https://regex101.com/r/tN7nW9/2

You should include your data in the future as a string (not image) so users can run tests on it.

PHP Usage:

echo preg_replace('/^\h*\/\/.*$/m', '', $string);

PHP Demo: https://eval.in/430182

chris85
  • 23,846
  • 7
  • 34
  • 51
  • hmm, you and the other guy have slightly different results. can you tell me what's the difference between THIS ~\\n? */{2,} .*~ AND THIS /^\h*\/\/.*$/m – Kevin Sep 08 '15 at 22:20
  • That looks like it says an optional new line, any amount of white spaces until two or more `/` characters then anything. Which could be dangerous. My regex is going line by line requiring the line start `//` (regardless of white space) and if so replacing the whole line with nothing. Again if you include your actual data this would be much easier, can just run regex101 with expressions. – chris85 Sep 09 '15 at 06:00
  • Did either answer worked for you @Kevin? http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – chris85 Sep 18 '15 at 16:53