在JavaScript中给出类似以下的字符串
var a = 'hello world\n\nbye world\n\nfoo\nbar\n\nfoo\nbaz\n\n';
我想把它分成像这样的数组
['hello world', '\n\n', 'bye world', '\n\n', 'foo\nbar', '\n\n', 'foo\nbaz', '\n\n'].
如果输入为var a = 'hello world\n\nbye world'
,则结果应为['hello world', '\n\n', 'bye world']
。
换句话说,我想将'\ n \ n'周围的字符串拆分成一个数组,这样数组也包含'\ n \ n'。在JavaScript中有没有任何巧妙的方法?
答案 0 :(得分:3)
这是一个班轮:
str.match(/\n\n|(?:[^\n]|\n(?!\n))+/g)
以下是它的工作原理:
\n\n
匹配两个连续的换行符(?:[^\n]|\n(?!\n))+
匹配任一个或多个字符的任何序列
[^\n]
不是换行符或\n(?!\n)
换行符,但前提是没有其他换行符这种递归模式可以应用于任何长度:
// 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))
更新以下是对String.prototype.split
的修改,该修改应添加包含匹配分隔符的功能:
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);
}
}
})();
}