Here’s a one liner:
str.match(/\n\n|(?:[^\n]|\n(?!\n))+/g)
Here’s how it works:
\n\n
matches the two consecutive newline characters
(?:[^\n]|\n(?!\n))+
matches any sequence of one or more character of either
[^\n]
not a newline character, or
\n(?!\n)
a newline character but only if not followed by another newline character
This recursive pattern can be applied on any length:
// useful function to quote strings for literal match in regular expressions
RegExp.quote = RegExp.quote || function(str) {
return (str+"").replace(/(?=[.?*+^$[\]\\(){}|-])/g, "\\");
};
// helper function to build the above pattern recursively
function buildRecursivePattern(chars, i) {
var c = RegExp.quote(chars[i]);
if (i < chars.length-1) return "(?:[^" + c + "]|" + c + buildRecursivePattern(chars, i+1) + ")";
else return "(?!" + c + ")";
}
function buildPattern(str) {
return RegExp(RegExp.quote(delimiter) + "|" + buildRecursivePattern(delimiter.match(/[^]/g), 0) + "+", "g");
}
var str = 'hello world\n\nbye world\n\nfoo\nbar\n\nfoo\nbaz\n\n',
delimiter = "\n\n",
parts;
parts = str.match(buildPattern(delimiter))
Update Here’s a modification for String.prototype.split
that should add the feature of containing a matched separator as well:
if ("a".split(/(a)/).length !== 3) {
(function() {
var _f = String.prototype.split;
String.prototype.split = function(separator, limit) {
if (separator instanceof RegExp) {
var re = new RegExp(re.source, "g"+(re.ignoreCase?"i":"")+(re.multiline?"m":"")),
match, result = [], counter = 0, lastIndex = 0;
while ((match = re.exec(this)) !== null) {
result.push(this.substr(lastIndex, match.index-lastIndex));
if (match.length > 1) result.push(match[1]);
lastIndex = match.index + match[0].length;
if (++counter === limit) break;
}
result.push(this.substr(lastIndex));
return result;
} else {
return _f.apply(arguments);
}
}
})();
}