Javascript / RegExp:Lookbehind断言导致“无效组”错误 [英] Javascript/RegExp: Lookbehind Assertion is causing a "Invalid group" error
问题描述
我正在做一个简单的Lookbehind Assertion来获取一段URL(例如下面的代码),但是没有得到匹配,我得到以下错误:
I'm doing a simple Lookbehind Assertion to get a segment of the URL (example below) but instead of getting the match I get the following error:
Uncaught SyntaxError: Invalid regular expression: /(?<=\#\!\/)([^\/]+)/: Invalid group
这是我正在运行的脚本:
Here is the script I'm running:
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。
Can anyone shed some light on why this regex group is causing this error? Looks like a valid RegEx to me.
我知道如何获得我需要的细分市场的替代品,所以这真的只是帮助我理解这里发生的事情而不是而不是获得替代解决方案。
I know of alternatives on how to get the segment I need, so this is really just about helping me understand what's going on here rather than getting an alternative solution.
感谢您阅读。
J。
推荐答案
我认为JavaScript不支持积极的外观。你将不得不做更多这样的事情:
I believe JavaScript does not support positive lookbehind. You will have to do something more like this:
<script>
var regex = /\#\!\/([^\/]+)/;
var url = "http://my.domain.com/index.php/#!/write-stuff/something-else";
var match = regex.exec(url);
alert(match[1]);
</script>
这篇关于Javascript / RegExp:Lookbehind断言导致“无效组”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!