为什么Javascript不支持lookbehind断言? [英] Why are lookbehind assertions not supported in Javascript?

查看:871
本文介绍了为什么Javascript不支持lookbehind断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我意识到(一些尴尬)正则表达式后瞻性断言 不是 可能在 Javascript

Recently I realized (by some embarrassment) that regex lookbehind assertions were not possible in Javascript.

这个断言缺席的(事实)原因是什么似乎很常见?

我意识到还有其他方法可以实现同样的目标,尽管工作中的基本语义是禁止功能还是究竟是什么?

I realize there are alternate ways to achieve the same thing perhaps, although Is it the basic semantics at work which forbid the functionality, or what exactly?

似乎还有一些正则表达式测试工具从正则表达式模式生成Javascript代码似乎忽略了这一事实 - 这让我感到有些奇怪。

It also seems that some regex testing tools out there that generate Javascript code from regex patterns seem to ignore this fact — which strikes me as a bit odd.

推荐答案

今天



Lookbehind现在是 ES 2018规范。 Axel Rauschmayer在他的博客文章中提供了好的介绍

Today

Lookbehind is now an official part of the ES 2018 specification. Axel Rauschmayer gives a good introduction in his blog post.

看起来当时,Brendan Eich并不知道它的存在(因为Netscape是基于旧版本的Perl构建的):

It looks like at the time, Brendan Eich wasn't aware of its existence (because Netscape was built on an older version of Perl):


这是1998年,我在97年做的Netscape 4工作基于Perl 4(!),但我们向ECMA TC39 TG1(JS组 - - 事情是不同的,包括大写字母)基于Perl 5的东西。我们没有得到所有东西,我们不得不合理化一些明显的怪癖。

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.

我不知道记得lookbehind(在1998年7月出现在Perl 5.005中)被故意排除在外。 Waldemar可能还记得更多,我已经把他的JS密钥递给了netscape.com,去了mozilla.org。

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.

如果你是写游戏或者迷你游戏的话。 spec(甚至是ES5的风格),让我知道。我将在下周与其他TC39人聊聊此事。

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

在邮件列表上有许多不同的尝试包含它,但它似乎仍然是一个非常复杂的功能性能,因为 EcmaScript正则表达式 回溯和回溯。如果使用不当,可能会导致灾难性回溯等问题。

There have been a bunch of different on the mailing list with attempts to include it, but it still seems to be a pretty complex feature performance-wise, because EcmaScript Regular Expressions are backtracking based and backtracking is needed in lookbehind when working with capturing groups. This can lead to problems such as catastrophic backtracking when used incorrectly.

在某些时候它被建议用于ES6 / Es 2015,但它从未制定过草案,更不用说规范了。在讨论的最后一篇文章中,似乎没有人承担实施它的任务。如果有人打电话来编写实现,他们可以注册 ES讨论列表和建议。

At some point it was suggested for ES6/Es 2015, but it never made the draft, let alone the specification. In the last post in the discussion, it seems that nobody took up the task of implementing it. If anybody feels called to write an implementation, they can sign up for the ES Discuss list and propose it.

2015年5月,NozomuKatō提出了ES7后视实施

Regex Look-behind被添加为第0阶段提案

Regex Look-behind was added as a stage 0 proposal.

该提案目前处于第3阶段。这意味着现在至少有两个浏览器需要实现它才能成为下一个EcmaScript标准的一部分。正如@martixy在评论中提到的那样, Chrome已经在JS实验标志后面实现了它

The proposal is now at stage 3. This means that now at least two browsers need to implement it for it to become a part of the next EcmaScript standard. As @martixy mentioned in the comments, Chrome has implemented it behind the JS experimental flag.

这篇关于为什么Javascript不支持lookbehind断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆