Skip to content

JavaScript Regex+ flavor #2299

Open
Open
@slevithan

Description

@slevithan

Flavor Request

I have a biased interest as the creator of the new regex library (which is a spiritual successor to the broadly-used XRegExp library which has been around since ES3 days), but still I think it would be significantly beneficial for JavaScript programmers, both people who are already using the regex library and people who would discover there is a better and more readable way to write native JavaScript regexes.

From its readme:

regex is a template tag that extends JavaScript regular expressions with features that make them more powerful and dramatically more readable. It returns native RegExp instances that equal or exceed native performance. It's also lightweight, supports all ES2024+ regex features, and can be used as a Babel plugin to avoid any runtime dependencies or added runtime cost.

Highlights include support for free spacing and comments, atomic groups via (?>…) that can help you avoid ReDoS, subroutines via \g<name> that enable powerful composition, and context-aware interpolation of regexes, escaped strings, and partial patterns.

With the regex package, JavaScript steps up as one of the best regex flavors alongside PCRE and Perl, and maybe surpassing C++, Java, .NET, and Python.

Implementation should be straightforward compared to any other new regex flavor because it's a lightweight library that runs on the client, and its features are a strict superset of native JavaScript regexes with flag v enabled. All of its extended features are already available in flavors that regex101 supports.

Following is a complete list of the changes that would be needed to support the JavaScript regex flavor, compared to the existing support for the "ECMAScript (JavaScript)" flavor:

  • Disable or remove flag u (unicode), since flag v is always enabled.
  • Flag v (vnicode) is always enabled.
  • Flag x (extended) is available and always enabled.
    • Whitespace (specifically space and tab) is also ignored within character classes). This works like PCRE's flag xx (which regex101 doesn't yet list as an option) and Java's x flag (which is supported by regex101, although Java allows any whitespace in character classes with flag x).
  • Flag n (non-capturing) is available and always enabled.
    • Works the same as flag n in .NET (which regex101 already supports as an option) and PCRE (where regex101 doesn't yet list it as an option).
    • Tooltip and explanation for syntax (…) should describe it as a non-capturing group, which is already supported by regex101 for the .NET flavor with flag n enabled.
    • Highlight all numbered backreferences (\1, etc.) as errors, since numbered backreferences to named groups are disabled by the always-on flag n (different from .NET, but the same as Ruby and C++ with its flag n equivalent nosubs).
    • Don't show numbered groups in the "Match Information" card; only named groups (due to flag n).
  • Highlight syntax (?>…) as an atomic group.
    • Works the same as (?>…) in PCRE, Java, and .NET, where regex101 already supports atomic groups.
  • Highlight syntax \g<name> as a subroutine.
    • Works the same as \g<name> in PCRE, where regex101 already supports subroutines. Edit: The only difference is it can't be used recursively.
  • Change the delimiter options to only include `.

There is detailed documentation for all of these features in the regex package's readme, and of course I would be happy to help however I can.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions