我需要使用JavaScript RegExp对象从JavaScript源中删除所有JavaScript注释.
我需要的是RegExp的模式.
到目前为止,我发现了这个:
compressed = compressed.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');
Run Code Online (Sandbox Code Playgroud)
此模式适用于:
/* I'm a comment */
Run Code Online (Sandbox Code Playgroud)
或者:
/*
* I'm a comment aswell
*/
Run Code Online (Sandbox Code Playgroud)
但似乎不适用于内联:
// I'm an inline comment
Run Code Online (Sandbox Code Playgroud)
我不是RegEx的专家和它的模式,所以我需要帮助.
另外,我想要一个RegEx模式,它将删除所有那些类似HTML的注释.
<!-- HTML Comment //--> or <!-- HTML Comment -->
Run Code Online (Sandbox Code Playgroud)
还有那些条件HTML注释,可以在各种JavaScript源中找到.
谢谢.
如何使用PHP删除JS注释?这个问题已更新:2013年11月4日回答:Alexander Yancharuk但是现在有一个问题.新代码:id = id.replace(/\//g,'');
这是我的例子:
<?php
$output = "
//remove comment
this1 //remove comment
this2 /* remove comment */
this3 /* remove
comment */
this4 /* * * remove
* * * *
comment * * */
this5 http://removecomment.com
id = id.replace(/\//g,''); //do not remove the regex //
";
$output = preg_replace( "/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:)\/\/.*))/", "", $output ); //Yancharuk's code/regex
// "/(?<!\:)\/\/(.*)\\n/ = my oldest code
echo nl2br($output);
?>
Run Code Online (Sandbox Code Playgroud)
我的问题;
这是输出,最近:
this1
this2
this3
this4
this5 http://removecomment.com
ID = …