我猜我们可以vars使用如下表达式分别捕获所需的两个:.+'(.+?)'\s+?[+\-*]+\s+([a-z]+).+我们当然可以简化这个表达式,但我不确定我们可能拥有的其他输入。爪哇import java.util.regex.Matcher;import java.util.regex.Pattern;final String regex = ".+'(.+?)'\\s+?[+\\-*]+\\s+([a-z]+).+";final String string = "eval('enterData.style.pixelTop=' + y);\n" + "eval('enterData.style.pixelTop=' + x);";final String subst = "$1$2;";final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);final Matcher matcher = pattern.matcher(string);// The substituted value will be contained in the result variablefinal String result = matcher.replaceAll(subst);System.out.println(result);测试const regex = /.+'(.+?)'\s+?[+\-*]+\s+([a-z]+).+/gm;const str = `eval('enterData.style.pixelTop=' + y);eval('enterData.style.pixelTop=' + x);`;const subst = `$1$2;`;// The substituted value will be contained in the result variableconst result = str.replace(regex, subst);console.log(result);演示正则表达式如果不需要此表达式,可以在regex101.com中对其进行修改/更改。正则表达式电路jex.im可视化正则表达式: