Proposal for exact matching and matching at a position in RegExp

Steve L. steves_list at hotmail.com
Fri Feb 12 23:06:01 PST 2010


2010/2/12 Andy Chu <andy at chubot.org>:

> Agree that \G breaks some logical barrier.  I like to have a mental
> model of the implementation internals, and \G breaks that a bit.
>
> If compatibility with Mozilla is not an issue, I actually prefer
> Python's approach of .search() vs. .match().  It's not a part of the
> regex; it's not a property of the regex; it's how you *apply* the
> regex to a string.

I think a lot of people (myself included) use a similar mental model, 
although it doesn't quite match the implementation details. But even 
according to this model, avoiding /y wouldn't keep ES regex flags pure as 
mere pattern attributes since ES already crossed that bridge with /g (and no 
one complains about it).

> Though as mentioned, gracefully upgrading with ES3-5 is an issue, so I
> could only think of .exec() and .execLeft() for a left-anchored match.

Unlike execLeft, /y or \G would be useful not only with regex.exec/test, but 
also with string.match/replace/split; and all this in
backwards-compatible fashion (unlike adding new arguments or methods). The 
name execLeft might also be misleading since I imagine it would cause global 
regexes to be lastIndex-anchored rather than left-anchored.

> One thing I didn't bring up is that Python actually has an "endpos"
> argument.  You do regex.search(s, 10, 20), and it will stop at
> position 20.  I couldn't think of a real use case for this.  But
> anyone can think of one, that might be a consideration and sway things
> in favor of separate methods.

Are you proposing that execLeft would have different arguments than exec? I 
would not be a fan of that idea. Regarding the pos and endPos arguments 
you've proposed:

- An endPos argument might be useful with the replace and match methods when
using global regexes, but it would probably have limited use-cases with exec
and makes no sense with execLeft (assuming endPos is the last position where
a match can start). If, rather, it's the position at or before where the
last match must end, it would offer nothing more than a *possible* slight 
performance improvement vs. regex.execLeft(str.slice(0, endPos)). Searching 
a sliced copy of the string would actually have the potential to perform 
*better* if it prevents having to complete a slow match attempt that goes 
beyond endPos before being discarded upon completion.

- A pos argument (which you mentioned elsewhere) is unnecessary and
confusing for test/exec/execLeft since the lastIndex property already
provides the same functionality (albeit with the requirement that it's used
with a global regex). I wish exec, test, replace, and match all offered a
pos argument and that the lastIndex property didn't exist, but that ship has
sailed.

I too like Python's regex methods, but I don't think that what Python does
well here can easily be dropped on top of ES5.

Steven Levithan
http://blog.stevenlevithan.com
 



More information about the es-discuss mailing list