Skip to content


Subversion checkout URL

You can clone with
Download ZIP


[Edit:] Add XRegExp.match #16

slevithan opened this Issue · 0 comments

1 participant


Edit: Original title: Add XRegExp.matchAll

Create a new function called XRegExp.matchAll. This will work the same as String.prototype.match with /g except for the following details:

  • Returns an empty array (rather than null) if no match is found.
  • Doesn't require /g. In other words, it works the same for regexes with or without the global flag, and never acts as an alias of exec.
  • Does not implicitly convert provided non-RegExp search values to regex objects. Instead, a TypeError is thrown.
  • Fixes any cross-browser bugs for the setting of lastIndex, compared to the native String.prototype.match. In other words, global regexes always have their lastIndex set to 0 upon completion, and non-global regexes never have their lastIndex modified from its original value. When using the native String.prototype.match with /g, IE (<= 8 ?) does not reset lastIndex to 0 upon completion.

This new function should also be mapped/aliased as XRegExp.prototype.matchAll in the XRegExp Prototype Methods addon.

Background details:

XRegExp v2.0.0 already includes a version of String.prototype.match with cross-browser lastIndex fixes, but it cannot be used without first running XRegExp.install('natives'). It does not include the other differences mentioned above. All other fixed/extended natives already have a corresponding XRegExp function that does not require overriding natives (XRegExp.exec/test/replace/split).

XRegExp doesn't need an equivalent of String.prototype.match without /g (i.e., match instead of matchAll), because that is already provided by XRegExp.exec. More details on the rationale for adding matchAll but not match can be found here.

String.prototype.match with /g is the last place where XRegExp users need to use flag /g or fiddle with lastIndex. With XRegExp.matchAll in place, XRegExp really will live up to its claim that it "frees you from worrying about pesky inconsistencies in cross-browser regex handling and the dubious lastIndex property."

@slevithan slevithan closed this in fac6851
@alexgorbatchev alexgorbatchev referenced this issue from a commit in syntaxhighlighter/xregexp
@slevithan added XRegExp.matchAll (closes #16) 5f90214
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.