slevithan / regex

Regex template tag for readable, high-performance, native JS regexes, with context-aware interpolation and always-on best practices
MIT License
470 stars 9 forks source link

Potential alternative for flags syntax #3

Closed rauschma closed 3 months ago

rauschma commented 3 months ago

(Very useful library, thanks for creating it!)

Another option for specifying RegExp flags is:

regex`/^.+/gm`

That is equivalent to:

regex('gm')`^.+`

Benefits:

One could make the slashes optional if there are no flags.

slevithan commented 3 months ago

Thanks, @rauschma! 😊

I agree with the advantages you outlined. But there are also some disadvantages:

I'd prefer to avoid ambiguity and conflict between flags by not adding support for the `/.../flags` format on top of the current approach (flags as an argument). But it would be easy to add a user-side wrapper that drops flags as an argument and supports flags via the string. 😊

rauschma commented 3 months ago
rauschma commented 3 months ago

(Aside: the content of your comment would make a great FAQ entry – it contains several interesting facts.)

slevithan commented 2 months ago

It took me a while, but per your suggestion, this is now covered by a new question in the FAQ.