preg_match - console.log 移除
preg_match - console.log removing
场景是这样的:
- 使用 file_get_contents
将 JS 文件加载到字符串中
- 我想从中删除所有调试信息
- 为了找出 PHP 代码中发生了什么,我
使用 preg_match
我正在使用这个表达式:
(\/\/)?(\s*?)console\.(log|debug|info|log|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\((.*?[^}(])\);?$
在 regex101 and phpliveregex 个匹配的网站上:
//console.log(abc)
// console.log(abc)
// console.log(abc);
// console.log('abc');
console.log(abc);
console.log('abc' + some_function());
etc...
但是当我把它放在 PHP 代码中时:
preg_match('/(\/\/)?(\s*?)console\.(log|debug|info|log|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\((.*?[^}(])\);?$/', $js_code, $matches);
if (!empty($matches[0])) print_r($matches[0]);
我没有得到任何匹配项。太累了,没注意到我错过了什么。大概有什么东西用它的大眼睛盯着我看。 :)
任何帮助将不胜感激。
为什么这么复杂?您是否需要区分不同的功能(log
,等等)?以下正则表达式匹配您上面的所有示例。在此处查看 working demo。
$regex = '/(?<console>(?:\/\/)?\s*console\.[^;]+;)/g';
# captured group named console with two forward slashes optionally
# followed by whitespaces (or not)
# match console. literally then anything up to a semicolon
preg_match_all($regex, $js_string, $matches);
print_r($matches["console"]);
根据您的评论,如果您还需要匹配实际的方法名称,您可以像这样更改正则表达式:
$regex = '/(?<console>(?:\/\/)?\s*console\.(?<function>[^(]+)[^;]+;)/g';
现在 $matches["function"]
保留实际的方法名称,参见 a demo for this here。
经过进一步调查,我改进了我的正则表达式模式以匹配每个组合。
@简
你的回答把我推向了正确的方向。
((\/\/)?(\s*?)console\.(log|debug|info|log|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)(\s*?)\((.*[^}(])(\){1,});?)
这就是我为解决您的问题所做的工作。希望它对你有用。
// DEFINE THE STRING
$string = "
<br>Other Text Goes Here
//console.log(abc)
// console.log(abc)
// console.log(abc);
// console.log('abc');
<br>More Text Here
console.log(abc);
console.warn('abc' + some_function());
console.log('abc' + some_function());
<br>And More Text Goes Here";
// DO THE PREG_MATCH_ALL TO FIND ALL OCCURRENCES
preg_match_all('~(?://)?\s*console\.[A-Z]+\(.*?$~sim', $string, $matches);
print "<pre>"; print_r($matches[0]); print "</pre>";
这将为您提供以下内容:
Array
(
[0] => //console.log(abc)
[1] => // console.log(abc)
[2] => // console.log(abc);
[3] => // console.log('abc');
[4] =>
console.log(abc);
[5] =>
console.warn('abc' + some_function());
[6] =>
console.log('abc' + some_function());
)
找到它们是一回事,但与实际用空字符串替换它的出现并没有太大区别。这样的事情应该可以解决问题:
print preg_replace('~((?://)?\s*console\.[A-Z]+\(.*?$)~sim', '', $string);
这将在浏览器中显示:
Other Text Goes Here
More Text Here
And More Text Goes Here
这里有一个工作演示供您查看:
解释:
(?://)?\s*console\.[A-Z]+\(.*?$
(?://)?
- 寻找可选的两个正斜杠。前面的?:
告诉它去找找,不记得了。
\s*
- 查找可能存在或不存在的任何空格。
console\.[A-Z]+
- 将匹配 console
,后跟文字点 .
,后跟至少一个字母字符。
\(.*?$
- 找到一个左括号并抓住行尾的所有内容。
场景是这样的:
- 使用 file_get_contents 将 JS 文件加载到字符串中
- 我想从中删除所有调试信息
- 为了找出 PHP 代码中发生了什么,我 使用 preg_match
我正在使用这个表达式:
(\/\/)?(\s*?)console\.(log|debug|info|log|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\((.*?[^}(])\);?$
在 regex101 and phpliveregex 个匹配的网站上:
//console.log(abc)
// console.log(abc)
// console.log(abc);
// console.log('abc');
console.log(abc);
console.log('abc' + some_function());
etc...
但是当我把它放在 PHP 代码中时:
preg_match('/(\/\/)?(\s*?)console\.(log|debug|info|log|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)\((.*?[^}(])\);?$/', $js_code, $matches);
if (!empty($matches[0])) print_r($matches[0]);
我没有得到任何匹配项。太累了,没注意到我错过了什么。大概有什么东西用它的大眼睛盯着我看。 :) 任何帮助将不胜感激。
为什么这么复杂?您是否需要区分不同的功能(log
,等等)?以下正则表达式匹配您上面的所有示例。在此处查看 working demo。
$regex = '/(?<console>(?:\/\/)?\s*console\.[^;]+;)/g';
# captured group named console with two forward slashes optionally
# followed by whitespaces (or not)
# match console. literally then anything up to a semicolon
preg_match_all($regex, $js_string, $matches);
print_r($matches["console"]);
根据您的评论,如果您还需要匹配实际的方法名称,您可以像这样更改正则表达式:
$regex = '/(?<console>(?:\/\/)?\s*console\.(?<function>[^(]+)[^;]+;)/g';
现在 $matches["function"]
保留实际的方法名称,参见 a demo for this here。
经过进一步调查,我改进了我的正则表达式模式以匹配每个组合。
@简
你的回答把我推向了正确的方向。
((\/\/)?(\s*?)console\.(log|debug|info|log|warn|error|assert|dir|dirxml|trace|group|groupEnd|time|timeEnd|profile|profileEnd|count)(\s*?)\((.*[^}(])(\){1,});?)
这就是我为解决您的问题所做的工作。希望它对你有用。
// DEFINE THE STRING
$string = "
<br>Other Text Goes Here
//console.log(abc)
// console.log(abc)
// console.log(abc);
// console.log('abc');
<br>More Text Here
console.log(abc);
console.warn('abc' + some_function());
console.log('abc' + some_function());
<br>And More Text Goes Here";
// DO THE PREG_MATCH_ALL TO FIND ALL OCCURRENCES
preg_match_all('~(?://)?\s*console\.[A-Z]+\(.*?$~sim', $string, $matches);
print "<pre>"; print_r($matches[0]); print "</pre>";
这将为您提供以下内容:
Array
(
[0] => //console.log(abc)
[1] => // console.log(abc)
[2] => // console.log(abc);
[3] => // console.log('abc');
[4] =>
console.log(abc);
[5] =>
console.warn('abc' + some_function());
[6] =>
console.log('abc' + some_function());
)
找到它们是一回事,但与实际用空字符串替换它的出现并没有太大区别。这样的事情应该可以解决问题:
print preg_replace('~((?://)?\s*console\.[A-Z]+\(.*?$)~sim', '', $string);
这将在浏览器中显示:
Other Text Goes Here
More Text Here
And More Text Goes Here
这里有一个工作演示供您查看:
解释:
(?://)?\s*console\.[A-Z]+\(.*?$
(?://)?
- 寻找可选的两个正斜杠。前面的?:
告诉它去找找,不记得了。\s*
- 查找可能存在或不存在的任何空格。console\.[A-Z]+
- 将匹配console
,后跟文字点.
,后跟至少一个字母字符。\(.*?$
- 找到一个左括号并抓住行尾的所有内容。