如何删除仅以“//”开头的JavaScript评论? [英] How do I remove ONLY JavaScript comments that start with "// "?
问题描述
首先,我已经有一个压缩工具,成功压缩其他所有,所以我不需要一个长的复杂 preg_replace
regex,只是一个简单的 preg_replace
或 str_replace
规则。
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.
我只想删除JavaScript以//开头的评论(包括空格,所以我不删除URL)直到新行。而已!再次,我不需要替换/ * * /或任何其他变体,因为我有一个脚本,这已经;它缺少的唯一的东西是这一个功能,所以我想清理输入与这第一,然后交给脚本。
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:
我真的很感激这个帮助! :-) THANKS!
I would really appreciate help with this! :-) THANKS!
推荐答案
尝试以下代码:
$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
这篇关于如何删除仅以“//”开头的JavaScript评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!