Node JS PCRE正则表达式并转换支持

时间:2016-04-15 10:09:34

标签: regex node.js npm pcre npm-install

我需要在多个反向引用中应用大量PCRE正则表达式,并在Node JS应用程序中应用perl样式转换s/\/\/(\d+)/$1/s

我四处寻找并为此目的找到了这两个库:

https://github.com/mscdex/node-pcre(正则表达式申请)

https://github.com/tokuhirom/node-perl(Perl样式转换)

我在Node v5.9.0中安装这些模块时遇到了很多NPM错误。

node-pcre库有issue,表示由于V8 API的更改,库在NodeJS 0.10版本之后停止工作

我也遇到了node-perl库的安装问题。

我是否有办法在Node v5.9.0中获得对PCRE正则表达式和转换的支持

由于

编辑:以下评论之一是指定正在尝试的PCRE正则表达式在javascript中不起作用。这是一个例子:

var fs = require("fs");

var regex = '<input type=\"hidden\"\s*name=\"itemId\"\s*value=\"(?P<sku>[\w\-]+)\"[^<]*>.*?<SCRIPT LANGUAGE=\'JavaScript\'[^<]*>.*?(\g{sku}Matrix\s*\=.*?<\/SCRIPT>)';
var page = fs.readFileSync("./page.html");

var re = new RegExp(regex);
var matchStr = "";

matchStr = page.replace(re, function (match, $1) { return $1; });
console.log(matchStr);

响应

SyntaxError: Invalid regular expression: /<input type="hidden"s*name="itemId"s*value="(?P<sku>[w-]+)"[^<]*>.*?<SCRIPT LANGUAGE='JavaScript'[^<]*>.*?(g{sku}Matrixs*=.*?</SCRIPT>)/: Invalid group
    at new RegExp (native)
    at Object.<anonymous> (/home/user/project/abc.js:7:10)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:142:18)
    at node.js:939:3

2 个答案:

答案 0 :(得分:1)

In the regex you gave, you are using named groups, which JS doesn't support. As a result, you'll need to use numbered groups, which pushes your desired capture to $2, in this case.

<input type=\"hidden\"\s*name=\"itemId\"\s*value=\"([\w\-]+)\"[^<]*>.*?<SCRIPT LANGUAGE=\'JavaScript\'[^<]*>.*?(\1Matrix\s*\=.*?<\/SCRIPT>)';

It should work like this:

matchStr = page.replace(re, function (match, $2) { return $2; });
  1. Find the group mentioned with your external $# statement. Remember which one it is.

  2. You'll need to count the capture groups, both named ((?P<NAME>)) and unnamed ()

    • ([abc])(?:[efg])(?P<NAME>[hij]) Remember, here [abc] is 1, and [hij] is 2 because (?:) is non capturing.
  3. Then, find any references to the named captures (\g<NAME>) and replace with \#, where # is the number from the last step.

  4. Change the $#s in your JS code after getting the new number for the group found in the first step.

答案 1 :(得分:0)

使用

sudo npm install perl
sudo npm install pcre
对我来说。可能与sys libs绑定/链接需要sudo / root权限。 上面的命令对我有用。

因此,如果其他人需要在通过nvm管理的最新节点上安装这些模块,只需使用sudo和其他依赖项安装这些模块,而无需像往常一样使用sudo。