JavaScript 正则表达式的线性匹配
Linear Matching of JavaScript Regular Expressions
AI总结:
本文识别了 JavaScript 正则表达式可线性匹配的更大子集,纠正了现有算法错误,并首次提出线性时间匹配环视的非回溯算法,部分已集成至 V8 引擎。
摘要:
现代正则表达式语言已远远偏离了人们充分理解的传统正则表达式:它们包含从根本上改变匹配问题的特性。为了这些特性,现代正则表达式引擎有时会遭受指数级复杂度爆炸,这是 JavaScript 应用程序中拒绝服务攻击的常见来源。更糟糕的是,正则表达式语义在不同语言间存在差异,而这些差异对算法设计和最坏情况匹配复杂度的影响很少被研究。本文通过识别 JavaScript 正则表达式语言中一个比先前理解的更大的子集(该子集可以保证在线性时间内匹配),为 JavaScript 的正则表达式语义提供了新颖的视角。在此过程中,我们发现了几种情况,其中现有最先进的算法要么是错误的(语义不正确),要么是低效的(遭受超线性复杂度),要么是过度限制的(假设某些特性无法线性匹配)。我们引入了新颖的算法来恢复正确性和线性复杂度。我们进一步推进了线性正则表达式匹配的最新技术水平,首次提出了用于在线性时间内匹配环视(lookarounds)的非回溯算法:一种支持任何正则表达式语言中的无捕获后顾(captureless lookbehinds),另一种利用 JavaScript 的属性来支持无限制的前瞻(lookaheads)和后顾(lookbehinds)。最后,我们描述了正则表达式引擎新的时间和空间复杂度权衡。我们所有的算法都是实用的:我们在原型实现中验证了它们,并且一些算法也已合并到用于 Chrome 和 Node.js 的 V8 JavaScript 实现中。
英文摘要:
Modern regex languages have strayed far from well-understood traditional regular expressions: they include features that fundamentally transform the matching problem. In exchange for these features, modern regex engines at times suffer from exponential complexity blowups, a frequent source of denial-of-service vulnerabilities in JavaScript applications. Worse, regex semantics differ across languages, and the impact of these divergences on algorithmic design and worst-case matching complexity has seldom been investigated. This paper provides a novel perspective on JavaScript's regex semantics by identifying a larger-than-previously-understood subset of the language that can be matched with linear time guarantees. In the process, we discover several cases where state-of-the-art algorithms were either wrong (semantically incorrect), inefficient (suffering from superlinear complexity) or excessively restrictive (assuming certain features could not be matched linearly). We introduce novel algorithms to restore correctness and linear complexity. We further advance the state-of-the-art in linear regex matching by presenting the first nonbacktracking algorithms for matching lookarounds in linear time: one supporting captureless lookbehinds in any regex language, and another leveraging a JavaScript property to support unrestricted lookaheads and lookbehinds. Finally, we describe new time and space complexity tradeoffs for regex engines. All of our algorithms are practical: we validated them in a prototype implementation, and some have also been merged in the V8 JavaScript implementation used in Chrome and Node.js.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。