javascript - 为什么 Javascript 不支持lookbehind断言?

标签 javascript regex

最近我意识到(由于一些尴尬)正则表达式lookbehind断言中不可能 JavaScript

这种看似常见的断言不存在的(事实)原因是什么?

I realize there are alternate ways to achieve the same thing perhaps ,尽管是工作中的基本语义禁止了该功能,还是到底是什么?

似乎还有一些regex testing tools从正则表达式模式生成 Javascript 代码的方法似乎忽略了这个事实——这让我觉得有点奇怪。

最佳答案

今天

Lookbehind 现已成为 ES 2018 specification 的官方部分。 Axel Rauschmayer 给出了 good introduction in his blog post .

历史

It looks like at the time, Brendan Eich wasn't aware of its existence (因为 Netscape 是基于旧版本的 Perl 构建的):

This was 1998, Netscape 4 work I did in '97 was based on Perl 4(!), but we proposed to ECMA TC39 TG1 (the JS group -- things were different then, including capitalization) something based on Perl 5. We didn't get everything, and we had to rationalize some obvious quirks.

I don't remember lookbehind (which emerged in Perl 5.005 in July '98) being left out on purpose. Waldemar may recall more, I'd handed him the JS keys inside netscape.com to go do mozilla.org.

If you are game to write a proposal or mini-spec (in the style of ES5 even), let me know. I'll chat with other TC39'ers next week about this.

/be

邮件列表上有很多不同的尝试将其包含在内,但从性能 Angular 来看,它仍然是一个相当复杂的功能,因为 EcmaScript Regular Expressionsbacktracking使用捕获组时,回溯需要基于和回溯。这可能会导致诸如 catastrophic backtracking 之类的问题。使用不当时。

在某个时候,它被建议用于 ES6/Es 2015,但它从未成为草案,更不用说规范了。在 last post in the discussion ,似乎没有人承担起实现它的任务。如果有人觉得需要编写一个实现,他们可以注册 ES Discuss list并提出建议。

2015 年 5 月更新:

2015年5月,Nozomu Katō has proposed an ES7 look-behind implementation .

2015 年 9 月更新:

正则表达式后视已添加为 stage 0 proposal .

2017 年 5 月更新:

The proposal is now at stage 3 。这意味着现在至少有两个浏览器需要实现它才能成为下一个 EcmaScript 标准的一部分。正如@martixy 在评论中提到的,Chrome has implemented it behind the JS experimental flag .

关于javascript - 为什么 Javascript 不支持lookbehind断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118815/

相关文章:

javascript - 值没有传递到类实例上

javascript - 如何在运行时修改 javascript 代码?

php - 在 php 中使用正则表达式将 url 转换为域

正则表达式提取

javascript - 如何将 javascript 中的相同函数关联到 <c : forEach>? 的乘法按钮

Javascript/html5 选择器

python - 如果开头只有一个斜杠,则模式必须成功

regex - 301 将所有博客文章重定向到新的子文件夹

javascript - jQuery:更改输入值不用于新添加

javascript - 正则表达式精确匹配 5 位数字