slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.3k stars 277 forks source link

XRegExp.tag, function to take full advantage of tagged literals #204

Closed wojpawlik closed 6 years ago

wojpawlik commented 6 years ago

Not a pull request, because I'm not familiar with the source code, structure and conventions of the project.

Take a look at this well commented gist to get the idea.

I use slightly improved* version of it in my own project.

* It's not yet perfect. Regexs' source should be wrapped in nonCapturingGroup as well.

It allows to use tagged template literals to create regexes to its fullest.

Using it looks a little unusual, but there needs to be a way to pass flags.

Improvements over XRegExp() and XRegExp.build():

  1. It dispatches to String.raw, so backslashes behave as in regex literals; it avoids the "Regexes, strings, and backslashes" issue.
  2. Interpolation is achieved via native syntax for interpolation.

The best name for this function I can come up with is XRegExp.tag.

@josephfrazier mentioned XRegExp.tag in #195. I haven't seen it mentioned in docs, is it a thing already?

josephfrazier commented 6 years ago

XRegExp.tag is present on the master branch, but I don't think there's been a release including it. It uses XRegExp.build, so I'm not sure if it behaves the same way as your implementation.

wojpawlik commented 6 years ago

Thanks for reply and link to the function, @josephfrazier.

I'm not sure if it behaves the same way as your implementation.

If XRegExp.build wraps the substitutions in non-capturing group (the code's too long to check that quickly), then the implementation in repo should behave the same way, maybe XRegExp.build takes care of some cases I haven't encountered.

I find it funny how I called usage of my version "a little unusual", and then I see a function doing the same with the exact same signature :). Turns out it is the way.

I don't think there's been a release including it.

Yeah... just checked.

There hasn't been any activity in the repo and releases on npm since months... and all those unhandled issues and PRs...

Is the repo abandoned? If so, do you know any fork which is maintained?

josephfrazier commented 6 years ago

If XRegExp.build wraps the substitutions in non-capturing group (the code's too long to check that quickly), then the implementation in repo should behave the same way

I'm pretty sure it does. From the comments in the code linked above: "Interpolated patterns are treated as atomic units when quantified". Here's the relevant test.

I find it funny how I called usage of my version "a little unusual", and then I see a function doing the same with the exact same signature :). Turns out it is the way.

Haha, great minds think alike! Another way to do it would be to require the template literal to start/end with a /, but allow flags to be after the final /. This could be a little more readable, but it's more complicated to implement.

There hasn't been any activity in the repo and releases on npm since months... and all those unhandled issues and PRs...

Is the repo abandoned? If so, do you know any fork which is maintained?

According to http://forked.yannick.io/slevithan/xregexp, @GerHobbelt has a fork at https://github.com/GerHobbelt/xregexp that was updated 2 months ago. I don't know anything further about it, though.

I'm not sure if anyone other than @slevithan has commit/publish permissions for this repo/package, but maybe we could convince them to add me or another contributor as a maintainer, in order to keep things moving forward.

slevithan commented 6 years ago

I'm not sure if anyone other than @slevithan has commit/publish permissions for this repo/package, but maybe we could convince them to add me or another contributor as a maintainer, in order to keep things moving forward.

It's just been me and @mathiasbynens until now. @josephfrazier has submitted a bunch of high quality pull requests, so I'm happy to give him push access to the repo. In fact, I've just done so.

@josephfrazier, feel free to make significant changes, but please be careful and hold new features to a high standard of accounting for all the edge cases that tend to come with regex syntax/behavior/APIs. :)

josephfrazier commented 6 years ago

Hey @GingerPlusPlus, just wanted to let you know that v4.0.0 has been published and it includes XRegExp.tag. See https://github.com/slevithan/xregexp/pull/205 for context.

wojpawlik commented 6 years ago

maybe XRegExp.build takes care of some cases I haven't encountered.

This is one case I haven't considered:

any backreferences within an interpolated regex are rewritten to work within the overall pattern.