我有一个字符串,例如
String src = "How are things today /* this is comment *\*/ and is your code /*\* this is another comment */ working?"
Run Code Online (Sandbox Code Playgroud)
我想从字符串中删除/* this is comment *\*/ 和/** this is another comment */子src串.
我尝试使用正则表达式但由于经验不足而失败.
我正在编写一个快速preg_replace来从CSS中删除注释.CSS注释通常具有以下语法:
/* Development Classes*/
/* Un-comment me for easy testing
(will make it simpler to see errors) */
Run Code Online (Sandbox Code Playgroud)
所以我试图杀死/*和*/之间的所有内容,如下所示:
$pattern = "#/\*[^(\*/)]*\*/#";
$replace = "";
$v = preg_replace($pattern, $replace, $v);
Run Code Online (Sandbox Code Playgroud)
没有骰子!它似乎在正斜杠上窒息,因为如果我将/ s取出模式,我可以删除注释文本.我尝试了一些更简单的模式,看看我是否可以丢失斜杠,但它们会保持原始字符串不变:
$pattern = "#/#";
$pattern = "/\//";
Run Code Online (Sandbox Code Playgroud)
关于为什么我似乎无法匹配那些斜杠的任何想法?谢谢!