@aclaymore comment about needing to expose a flags attribute I think is a point against having inline flags for flags such as "g", as these really should be an on-off switch for the whole regex. I don't have a problem with having inline flags for things such as case sensitivity though, especially since it's technically possible to construct a case-insensitive regex without using that flag.
I did a lookaround at all of the different regular expression flags. Here are the ones that change how the regular expresion is interpretted:
-
i: Case insinsitive
-
m: multiline
-
s: Single line (dotall)
-
u: unicode
And here are the ones that ones that relate to how the search is performed and (and really should have been function parameters)
-
d: has indicies
-
g: global
-
y: sticky
It's this second group of flags that I'm starting to think we shouldn't allow as an inline flag, as they don't make sense when only applied to a portion of a regular expression, which means we must provide some other way to apply these flags.
I started looking around at some of the built-in functions that use regular expressions. Here are some that I found:
In all of these functions, it would be easy enough to add an optional options object argument to the end that accepted certain properties to configure how the search is performed. e.g. { global: true, hasIndicies: false }
.
The matchAll() function is particularly interesting. because 1. It's a newer function, and 2. In the documentation, I noticed that they will throw an error if you supply a regex that does not have a "g" flag. There seems to be no reason for this enforcement, except to require consistency with how the flags work. They could have alternativly implemented it to ignore the "g" flag on the regular expression, and always do a "global search" anyways, similar to what we are proposing should happen if "global" is set to true on an options argument for one of these other functions, but they decided against this. In other words, they seem to be upholding the idea of having flags such as "d", "g", and "y" be part of the regular expression instead of function parameters, so we'll probably have a difficult time changing this.
I think we would have a much easier time getting this proposal through if we followed suit (at least in the initial draft - conversations about this could certainly happen afterwards). Changing all of these functions would bloat the proposal a good amount anyways.
This leaves us with needing a convenient way to apply flags such as "g" and "d" to a template-string regular expression
- Doing
new RegExp(existingRegex, newFlags)
was a good idea, but probably only works well if we didn't need to set flags often. I would be in favor of just using this approach if we do alter the different search functions.
- There was the
Regex.from(<flags>)`the regex`
idea that I'm still a fan of
-
Regex.from.d`the regex`
was also brought up. This feels a little weird when combining multiple flags though: regex.from.d.g.i`the regex`