移除 /Strip 不需要的函数 (preg_replace)

Remove /Strip unwanted function (preg_replace)

例如,我们有这样的文本:

    // comments
    someFunc.f.log({
      obj:obj,
      other:other
    });
    console.log('here');
    someFunc.f.log({
      obj:obj,
      other:other
    }
);
    console.log('here');
    // comments

我想要这个文本条 someFunc.f.log(); PHP 后端和输出中的函数 get:

// comments
console.log('here');
console.log('here');
// comments

我们如何才能达到这一点?

如果没有嵌套的括号,你可以尝试 this regex regex101

$str = preg_replace('/^\h*someFunc\.f\.log\([^)]*\);\R*|^\h+/m', "", $str);

like this demo at eval.in. If there's nested parentheses, try with that recursive regex regex101

'/^\h*someFunc\.f\.log(\((?>[^)(]*(?1)?)*\));\R*|^\h+/m'

like another demo at eval.in

  • ^ 匹配行首 m 多行 flag
  • |是交替的管道符号
  • \h 匹配水平 space
  • [^...打开取反字符class
  • (?1) 粘贴第一个带括号的 subpattern
  • \R 匹配任何换行序列

(更多解释和代码生成器可在 regex101 获得)