我正在做一个简单的 Lookbehind Assertion 来获取 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
。
谁能解释一下为什么这个正则表达式组会导致这个错误?对我来说看起来像一个有效的正则表达式。
我知道关于如何获得我需要的分割市场的替代方案,所以这实际上只是帮助我了解这里发生的事情,而不是获得替代解决方案。
感谢阅读。
J.
最佳答案
我认为 JavaScript 不支持正面回顾。你将不得不做更多像这样的事情:
<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/正则表达式 : Lookbehind Assertion is causing a "Invalid group" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5973669/