//============================================================================1// Cross-browser RegEx Split2//============================================================================34// This code has been MODIFIED from the code licensed below to not replace the5// default browser split. The license is reproduced here.67// see http://blog.stevenlevithan.com/archives/cross-browser-split for more info:8/*!9* Cross-Browser Split 1.1.110* Copyright 2007-2012 Steven Levithan <stevenlevithan.com>11* Available under the MIT License12* ECMAScript compliant, uniform cross-browser split method13*/1415/**16* Splits a string into an array of strings using a regex or string17* separator. Matches of the separator are not included in the result array.18* However, if `separator` is a regex that contains capturing groups,19* backreferences are spliced into the result each time `separator` is20* matched. Fixes browser bugs compared to the native21* `String.prototype.split` and can be used reliably cross-browser.22* @param {String} str String to split.23* @param {RegExp} separator Regex to use for separating24* the string.25* @param {Number} [limit] Maximum number of items to include in the result26* array.27* @returns {Array} Array of substrings.28* @example29*30* // Basic use31* regex_split('a b c d', ' ');32* // -> ['a', 'b', 'c', 'd']33*34* // With limit35* regex_split('a b c d', ' ', 2);36* // -> ['a', 'b']37*38* // Backreferences in result array39* regex_split('..word1 word2..', /([a-z]+)(\d+)/i);40* // -> ['..', 'word', '1', ' ', 'word', '2', '..']41*/42exports.regex_split = function (str, separator, limit) {43var output = [],44flags = (separator.ignoreCase ? "i" : "") +45(separator.multiline ? "m" : "") +46(separator.extended ? "x" : "") + // Proposed for ES647(separator.sticky ? "y" : ""), // Firefox 3+48lastLastIndex = 0,49separator2, match, lastIndex, lastLength;50// Make `global` and avoid `lastIndex` issues by working with a copy51separator = new RegExp(separator.source, flags + "g");5253var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined";54if (!compliantExecNpcg) {55// Doesn't need flags gy, but they don't hurt56separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);57}58/* Values for `limit`, per the spec:59* If undefined: 4294967295 // Math.pow(2, 32) - 160* If 0, Infinity, or NaN: 061* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;62* If negative number: 4294967296 - Math.floor(Math.abs(limit))63* If other: Type-convert, then use the above rules64*/65limit = typeof(limit) === "undefined" ?66-1 >>> 0 : // Math.pow(2, 32) - 167limit >>> 0; // ToUint32(limit)68for (match = separator.exec(str); match; match = separator.exec(str)) {69// `separator.lastIndex` is not reliable cross-browser70lastIndex = match.index + match[0].length;71if (lastIndex > lastLastIndex) {72output.push(str.slice(lastLastIndex, match.index));73// Fix browsers whose `exec` methods don't consistently return `undefined` for74// nonparticipating capturing groups75if (!compliantExecNpcg && match.length > 1) {76match[0].replace(separator2, function () {77for (var i = 1; i < arguments.length - 2; i++) {78if (typeof(arguments[i]) === "undefined") {79match[i] = undefined;80}81}82});83}84if (match.length > 1 && match.index < str.length) {85Array.prototype.push.apply(output, match.slice(1));86}87lastLength = match[0].length;88lastLastIndex = lastIndex;89if (output.length >= limit) {90break;91}92}93if (separator.lastIndex === match.index) {94separator.lastIndex++; // Avoid an infinite loop95}96}97if (lastLastIndex === str.length) {98if (lastLength || !separator.test("")) {99output.push("");100}101} else {102output.push(str.slice(lastLastIndex));103}104return output.length > limit ? output.slice(0, limit) : output;105};106107//============================================================================108// End contributed Cross-browser RegEx Split109//============================================================================110111112