Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegExp lookbehind assertions #171

Closed
mathiasbynens opened this issue Sep 30, 2022 · 6 comments
Closed

RegExp lookbehind assertions #171

mathiasbynens opened this issue Sep 30, 2022 · 6 comments
Labels
accepted An accepted proposal focus-area-proposal Focus Area Proposal

Comments

@mathiasbynens
Copy link

mathiasbynens commented Sep 30, 2022

Description

Lookarounds are zero-width assertions that match a string without consuming anything. ECMAScript has long supported lookahead assertions that do this in forward direction. Positive lookahead ensures a pattern is followed by another pattern, and negative lookahead ensures a pattern is not followed by another pattern.

ES2018 added support for lookbehind assertions, which are lookarounds that go backwards. Positive lookbehind ensures a pattern is preceded by another pattern, and negative lookbehind ensures a pattern is not preceded by another pattern.

Rationale

Lookbehinds enable developers to ensure that a pattern is or isn’t preceded by another, which is a common use case when dealing with regular expressions — e.g. matching a dollar amount without capturing the dollar sign:

// Positive lookbehind:
const pattern = /(?<=\$)\d+/;
const result = pattern.exec('$42');
// → result[0] === '42'

Or, conversely, matching numbers that aren’t dollar amounts:

// Negative lookbehind:
const pattern = /(?<!\$)\d+/;
const result = pattern.exec('€42');
// → result[0] === '42'

Unlike many other JavaScript language features, lookbehind support cannot accurately be polyfilled or transpiled. Only interoperable engine-level support can fill the gap.

Chrome 62 (stable in 2017-10) and Firefox 78 (stable in 2020-06) have shipped support, but until all browsers provide support, compatibility issues continue to pop up. Examples:

Specification

https://tc39.es/ecma262/#prod-Assertion

Tests

https://test262.report/browse/built-ins/RegExp/lookBehind?engines=javascriptcore%2Cspidermonkey%2Cv8 (corresponding to https://github.com/tc39/test262/tree/main/test/built-ins/RegExp/lookBehind)

Note that since there are no other Interop proposals that need tests from Test262, it would be overkill to set up full Test262 integration just for this one feature. If this proposal is accepted, I volunteer to write equivalent WPT tests for this feature.

Update: web-platform-tests/wpt#37928

@mathiasbynens mathiasbynens added this to the Interop 2023 milestone Sep 30, 2022
@mathiasbynens mathiasbynens added the focus-area-proposal Focus Area Proposal label Sep 30, 2022
@foolip
Copy link
Member

foolip commented Sep 30, 2022

@karlcow
Copy link

karlcow commented Oct 3, 2022

to note that one of the reasons that Firefox implemented it was webcompat pressure. such as steam community chat

@gsnedders
Copy link
Member

gsnedders commented Oct 3, 2022

Note that since there are no other Interop proposals that need tests from Test262

#169 would beg to differ 🙃 (plus, of course, several weeks remaining for others to submit such proposals!)

@mathiasbynens
Copy link
Author

Note that since there are no other Interop proposals that need tests from Test262

#169 would beg to differ 🙃 (plus, of course, several weeks remaining for others to submit such proposals!)

I stand corrected :) Regardless, my offer to write WPT tests if needed still stands.

@foolip foolip moved this to Proposed in Interop 2023 Oct 7, 2022
@foolip foolip removed this from the Interop 2023 milestone Oct 7, 2022
@tbondwilkinson
Copy link

RegExp lookbehind assertions is a P2 for Google's closure library

"Consolidate functionality across browsers; potentially replace some string library usage and generally improve code health if used appropriately."

@nairnandu nairnandu added the accepted An accepted proposal label Feb 1, 2023
@nairnandu
Copy link
Contributor

Thank you for proposing RegExp lookbehind assertions for inclusion in Interop 2023.

We are pleased to let you know that this proposal was accepted as part of the Web Compat 2023 focus area. You can follow the progress of this Focus Area on the Interop 2023 dashboard.

For an overview of our process, see the proposal selection summary. Thank you for contributing to Interop 2023!

Posted on behalf of the Interop team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted An accepted proposal focus-area-proposal Focus Area Proposal
Projects
No open projects
Status: Proposed
Development

No branches or pull requests

6 participants