Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39537
1
//============================================================================
2
// Cross-browser RegEx Split
3
//============================================================================
4
5
// This code has been MODIFIED from the code licensed below to not replace the
6
// default browser split. The license is reproduced here.
7
8
// see http://blog.stevenlevithan.com/archives/cross-browser-split for more info:
9
/*!
10
* Cross-Browser Split 1.1.1
11
* Copyright 2007-2012 Steven Levithan <stevenlevithan.com>
12
* Available under the MIT License
13
* ECMAScript compliant, uniform cross-browser split method
14
*/
15
16
/**
17
* Splits a string into an array of strings using a regex or string
18
* separator. Matches of the separator are not included in the result array.
19
* However, if `separator` is a regex that contains capturing groups,
20
* backreferences are spliced into the result each time `separator` is
21
* matched. Fixes browser bugs compared to the native
22
* `String.prototype.split` and can be used reliably cross-browser.
23
* @param {String} str String to split.
24
* @param {RegExp} separator Regex to use for separating
25
* the string.
26
* @param {Number} [limit] Maximum number of items to include in the result
27
* array.
28
* @returns {Array} Array of substrings.
29
* @example
30
*
31
* // Basic use
32
* regex_split('a b c d', ' ');
33
* // -> ['a', 'b', 'c', 'd']
34
*
35
* // With limit
36
* regex_split('a b c d', ' ', 2);
37
* // -> ['a', 'b']
38
*
39
* // Backreferences in result array
40
* regex_split('..word1 word2..', /([a-z]+)(\d+)/i);
41
* // -> ['..', 'word', '1', ' ', 'word', '2', '..']
42
*/
43
exports.regex_split = function (str, separator, limit) {
44
var output = [],
45
flags = (separator.ignoreCase ? "i" : "") +
46
(separator.multiline ? "m" : "") +
47
(separator.extended ? "x" : "") + // Proposed for ES6
48
(separator.sticky ? "y" : ""), // Firefox 3+
49
lastLastIndex = 0,
50
separator2, match, lastIndex, lastLength;
51
// Make `global` and avoid `lastIndex` issues by working with a copy
52
separator = new RegExp(separator.source, flags + "g");
53
54
var compliantExecNpcg = typeof(/()??/.exec("")[1]) === "undefined";
55
if (!compliantExecNpcg) {
56
// Doesn't need flags gy, but they don't hurt
57
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
58
}
59
/* Values for `limit`, per the spec:
60
* If undefined: 4294967295 // Math.pow(2, 32) - 1
61
* If 0, Infinity, or NaN: 0
62
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
63
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
64
* If other: Type-convert, then use the above rules
65
*/
66
limit = typeof(limit) === "undefined" ?
67
-1 >>> 0 : // Math.pow(2, 32) - 1
68
limit >>> 0; // ToUint32(limit)
69
for (match = separator.exec(str); match; match = separator.exec(str)) {
70
// `separator.lastIndex` is not reliable cross-browser
71
lastIndex = match.index + match[0].length;
72
if (lastIndex > lastLastIndex) {
73
output.push(str.slice(lastLastIndex, match.index));
74
// Fix browsers whose `exec` methods don't consistently return `undefined` for
75
// nonparticipating capturing groups
76
if (!compliantExecNpcg && match.length > 1) {
77
match[0].replace(separator2, function () {
78
for (var i = 1; i < arguments.length - 2; i++) {
79
if (typeof(arguments[i]) === "undefined") {
80
match[i] = undefined;
81
}
82
}
83
});
84
}
85
if (match.length > 1 && match.index < str.length) {
86
Array.prototype.push.apply(output, match.slice(1));
87
}
88
lastLength = match[0].length;
89
lastLastIndex = lastIndex;
90
if (output.length >= limit) {
91
break;
92
}
93
}
94
if (separator.lastIndex === match.index) {
95
separator.lastIndex++; // Avoid an infinite loop
96
}
97
}
98
if (lastLastIndex === str.length) {
99
if (lastLength || !separator.test("")) {
100
output.push("");
101
}
102
} else {
103
output.push(str.slice(lastLastIndex));
104
}
105
return output.length > limit ? output.slice(0, limit) : output;
106
};
107
108
//============================================================================
109
// End contributed Cross-browser RegEx Split
110
//============================================================================
111
112