Javascript/正则表达式 : Lookbehind Assertion is causing a "Invalid group" error

标签 javascript jquery regex match lookbehind

我正在做一个简单的 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/

相关文章:

jquery - 如何在没有悬停的情况下对齐我的内容?

javascript - 不匹配特定大小写的正则表达式

javascript - 类型错误 : state is not iterable

javascript - 带有 React : Unexpected token '<' in call to renderToString() 的 SSR

javascript - AngularJS $http POST 转为 GET

javascript - 将 JavaScript 连接到 MySQL 数据库

javascript - 预加载器不预加载并第二次加载页面

javascript - 是否有一种好的跨浏览器方法来检测对 &lt;input&gt; 的更改?

Objective-C NSRegularExpressions,查找字符串中数字的第一次出现

Java正则表达式否定前瞻错误匹配