相关疑难解决方法(0)

用于匹配/替换JavaScript注释的RegEx(多行和内联)

我需要使用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源中找到.

谢谢.

javascript regex comments replace

38
推荐指数
5
解决办法
4万
查看次数

如何使用PHP删除JS注释?

如何使用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)

我的问题;

  1. 这一行有问题;
  2. //注释正在运行,但我无法创建代码来删除/*注释*/或带有换行符的注释

这是输出,最近:



this1
this2
this3
this4
this5 http://removecomment.com
ID = …

javascript php

10
推荐指数
3
解决办法
1万
查看次数

标签 统计

javascript ×2

comments ×1

php ×1

regex ×1

replace ×1