Browser fixes

Splitting on regex matches

There are several cross-browser inconsistencies when using a regular expression as the delimiter with the native String.prototype.split method. Divergences from the ES3/5 standard are listed below.

XRegExp.split provides a uniform cross-browser implementation that precisely follows the ES3/5 split specification. You can apply these fixes to the native String.prototype.split method by running XRegExp.install('natives').

lastIndex updates

There are a number of cross-browser inconsistencies between how the lastIndex property of regular expression objects is incremented and reset. This can cause problems even if you never manually check or set lastIndex in your code, because the RegExp.prototype.exec and RegExp.prototype.test methods use lastIndex to determine the character index where their search should begin (for regexes that use the /g flag). Regexes that use the /y flag (Firefox 3+) have all matches anchored to lastIndex. Several of the divergences from ES3/5 are listed below.

Using XRegExp.replace, XRegExp.exec, and XRegExp.test avoids all cross-browser lastIndex issues. Running XRegExp.install('natives') applies these fixes to the native methods.

Nonparticipating capture values

In Internet Explorer, captured values for nonparticipating capturing groups are incorrectly returned by RegExp.prototype.exec and String.prototype.match as an empty string rather than undefined, making it impossible to determine group participation. Firefox splices empty strings instead of undefined into the array returned by String.prototype.split as the result of nonparticipating capturing groups.

Using XRegExp.exec and XRegExp.split avoids all related cross-browser issues. Running XRegExp.install('natives') applies these fixes to the native methods.

Character class syntax

Traditional regex behavior is that a leading, unescaped ] within a character class is treated as a literal character and does not end the character class. However, this is not true for ES3/5, which states that [] is an empty set that will never match (similar to (?!)) and [^] matches any single character (like [\s\S] or [\0-\uFFFF]). Internet Explorer ≤ 8 and older versions of Safari use the traditional behavior, rather than the correct ES3/5 behavior. Older versions of Opera reverse the correct ES3/5 behavior, so that [] matches any character and [^] never matches.

Regexes created by XRegExp follow the ES3/5 standard behavior cross-browser.

Replacement text syntax

Internet Explorer ≤ 8 incorrectly treats all occurrences of $ as literal text when performing a replacement based on a search value that is not a regular expression.

XRegExp restores the special meaning of $$, $&, etc. for all replacements.