在Node.js中实现更好的正则expression式的方法

我为一个项目使用Node.js,我发现Javascript的正则expression式非常有限。 具体来说,缺乏逆向就是在杀我。 我试图用正则expression式来把stringparsing成句子,但我想检查先生和夫人等常用的缩写,这样我就不用打断了。 他们是一个Node.js库,添加了正则expression式function,如果不是,一个好的行动是什么?

这是JavaScript正则expression式的困难,

一种避免你的具体问题的方法:

 /((?:Mrs?\.)|[^\.]+)+/ # match all that is not a dot or Mr. or Mrs. 

对于更多的技巧,你可以看看这个网站: http : //blog.stevenlevithan.com/archives/javascript-regex-lookbehind

Node.js基于v8引擎,它的正则expression式引擎是v8的一部分。 v8项目托pipe在这里: https : //code.google.com/p/v8/ 。 正则expression式引擎来自这个文件: https : //code.google.com/p/v8/source/browse/trunk/src/ia32/regexp-macro-assembler-ia32.cc?r=4966 。 你可以主要分叉项目,并添加所需的function。 我怀疑这会比值得付出更多的努力。

正则expression式通常不用于parsing。 Node.js有很多parsing库可以在这里find: https ://npmjs.org/search?q=language+parsing。 我可以亲自推荐热cocoa( https://github.com/olleicua/hot-cocoa ),因为我自己做的,它完全符合我的目的。

最后,如果你的目标只是匹配任何一个单词或两个单词,如果第一个是“先生”或“夫人”,那么这样的事情可能会起作用:

 var text = 'Mr Potter and Mrs Smith were walking to the house of Mrs Sullivan'; text.match(/(?:Mr |Mrs )?\w+/g); // returns: [ 'Mr Potter', 'and', 'Mrs Smith', 'were', 'walking', 'to', 'the', // 'house', 'of', 'Mrs Sullivan' ]