-
Notifications
You must be signed in to change notification settings - Fork 2
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
Template tag as an alternative, that includes x plus additional improvements #8
Comments
Hi @slevithan , I am interested in this, but I cannot take the time to lead, or to write the spec text. I am also interested in your "atomic groups" Attn @waldemarhorwat |
There is plenty of value in
I am already championing a proposal for this that is at Stage 1: https://github.com/tc39/proposal-regexp-atomic-operators. I plan to invest more time in |
Good to know. Thanks.
Awesome! I look forward to this. Is it just coincidence that you both chose |
Long term, I would like to propose a template tag mechanism along with reintroducing prefix flags. Prefix flags was originally part of https://github.com/tc39/proposal-regexp-modifiers, but was cut. Prefix flags ( const re = RegExp`(?xu)
# comment
\p{LC}
`; vs const re = RegExp.tag("xu")`
# comment
\p{LC}
`;
// or
const re = RegExp`
# comment
\p{LC}
`("xu"); The version that the modifiers proposal originally had allowed prefix modifiers anywhere in the RegExp, though some implementations in other languages only allow them at the start of the RegExp. There was a suggestion to use const re = RegExp`/
pattern
/xu`; But the upshot to using a prefix |
There is precedent in many RegExp grammars: https://rbuckton.github.io/regexp-features/features/non-backtracking-expressions.html Maintaining this precedent helps developers port knowledge from other languages, as well as to reuse RegExp patterns defined in external files like in JSON or YAML, especially for editors using TextMate grammars, where those editors can be written in many different languages (Java, C#, C++, JS, etc.) |
I've opted to spread out the RegExp proposals since a single large proposal was originally rejected. Since |
I agree that there is value despite the limitations and the existence of multiline template strings. As I mentioned in my initial comment, a tag with always-on
That second point might be meaningful if flag
Yes, but no other flavors require escaping special characters even within comments. That's NOT fatal to
Awesome!
Modifier flags have the downside that they probably shouldn't be overwritten when copying a regex with new flags, since they are part of the pattern (reinforced by how most regex flavors that allow non-enclosed mode modifiers allow them at any position). So while
Glad to know nevertheless that you're interested in the ideas! |
The limitations with the
x
proposal (no multiline literals, and some special chars must be escaped even within comments) seem to meaningfully reduce the value ofx
. Several others have expressed similar concerns in previous issues. Additionally,x
mode would multiply the four existing ways to interpret escaped characters and parse regex syntax (Unicode-unaware mode, named capture mode,u
mode, andv
mode). The value ofx
as a standalone feature is also moderated by the existing ability to use a poor-man'sx
via:So, given both the limitations and the existence of multiline raw template literals, I'm questioning the value of a flag
x
when there might be a better path via a standardized template tag.A tag has been repeatedly discussed in the context of proposal https://github.com/tc39/proposal-regex-escaping, but I think it's more relevant as an alternative to
x
.A template tag, in addition to offering context-aware escaping, could potentially offer always-on
x
semantics (thereby encouraging best practices and not multiplying the number of modes to interpret escapes), without the limitations ofx
as a flag. And it could offer a great opportunity to clean up and modernize some aspects of JS regexes, providing a clear new happy path without JS regex footguns and legacy or the need to continually opt-in to best practices (apart from using the tag). This could be considered similar in approach to flagsu
andv
bundling multiple changes together to modernize and future-proof the syntax. And given that the stricter errors of those modes already offer fairly strong syntax future-proofing, the risks of needing another round of opt-in modernization after adding a tag would be relatively low likelihood, although I could expound on what the potential future changes might be that would bring this into question.Leaving that aside for now, here is what I would like to see in a standardized template tag:
\\\\
.RegExp
instances that preserve their local flags.x
, with or without flagx
being available to theRegExp
constructor and regex literals (not mutually exclusive).v
.n
("no auto capture" or "explicit capture" mode from .NET, PCRE, Perl, C++, XRegExp, etc.), with or without flagn
being available toRegExp
and regex literals (not mutually exclusive).I recently created the
regex
package that does all of these things (among other features), partly to help start a discussion around this. I think the semantics ofregex
(minus the syntax extensions) could serve as a starting point for a tag proposal. If there is interest in this, as well as another champion, I would be happy to contribute/collaborate.The text was updated successfully, but these errors were encountered: