Javascript / RegExp:Lookbehind Assertion导致“无效组”错误
我正在做一个简单的Lookbehind断言来获取URL的一部分(下面的示例)但是没有得到匹配,我得到以下错误:
Uncaught SyntaxError: Invalid regular expression: /(?<=\#\!\/)([^\/]+)/: Invalid group
这是我正在运行的脚本:
var url = window.location.toString();
url ==
http://my.domain.com/index.php/#!/write-stuff/something-else
// lookbehind to only match the segment after the hash-bang. var regex = /(?<=\#\!\/)([^\/]+)/i; console.log('test this url: ', url, 'we found this match: ', url.match( regex ) );
结果应该是write-stuff
。
任何人都可以解释为什么这个正则表达式组导致此错误? 对我来说看起来像一个有效的RegEx。
我知道如何获得我需要的细分市场的替代方案,所以这真的只是帮助我理解这里发生的事情而不是获得替代解决方案。
谢谢阅读。
J.
我相信JavaScript不支持积极的外观。 你将不得不做更多这样的事情:
Javascript不支持look-behind语法,因此(?<=)
是导致无效性错误的原因。 但是,您可以使用各种技术模仿它: http : //blog.stevenlevithan.com/archives/mimic-lookbehind-javascript