Skip to content

RegExp lookbehind assertions #171

Closed
Closed

Description

@mathiasbynens

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

Activity

  1. added this to the milestone on Sep 30, 2022
  2. karlcow commented on Oct 3, 2022

    @karlcow

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

  3. gsnedders commented on Oct 3, 2022

    @gsnedders
    Member

    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!)

  4. mathiasbynens commented on Oct 4, 2022

    @mathiasbynens
    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.

  5. moved this to Proposed in Interop 2023on Oct 7, 2022
  6. removed this from the milestone on Oct 7, 2022
  7. tbondwilkinson commented on Oct 24, 2022

    @tbondwilkinson

    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."

  8. added
    accepted
    on Feb 1, 2023
  9. nairnandu commented on Feb 1, 2023

    @nairnandu
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedfocus-area-proposal

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions