例如,我们有一些这样的文本:
// comments
someFunc.f.log({
obj:obj,
other:other
});
console.log('here');
someFunc.f.log({
obj:obj,
other:other
}
);
console.log('here');
// comments
我想从这个文本条中获取一些Func.f.log(); PHP 后端和输出中的函数得到:
// comments
console.log('here');
console.log('here');
// comments
我们如何才能达到这一点?
如果没有嵌套的括号,你可以尝试使用这个正则表达式 regex101
$str = preg_replace('/^'h*someFunc'.f'.log'([^)]*');'R*|^'h+/m', "", $str);
就像 eval.in 的这个演示一样。如果有嵌套括号,请尝试使用递归正则表达式 regex101
'/^'h*someFunc'.f'.log('((?>[^)(]*(?1)?)*'));'R*|^'h+/m'
就像 eval.in 的另一个演示一样
-
^
将行首与多行标志匹配m
-
|
是交替的管道符号 -
'h
匹配水平空间 -
[^
......打开否定字符类 -
(?1)
粘贴第一个带括号的子图案 -
'R
匹配任何换行符序列
(更多解释和代码生成器可在 regex101 中找到)