鉴于此文:
1/12/2011
I did something.
10/5/2013
I did something else.
Here is another line.
And another.
5/17/2014
Lalala.
More text on another line.
我想使用正则表达式(或者其他一些方法?)来获得这个:
["1/12/2011", "I did something.", "10/5/2013", "I did something else.\n\nHere is another line.\n\nAnd another.", "5/17/2014", "Lalala.\nMore text on another line."]
日期部分和内容部分是每个单独的条目,交替显示。
我尝试使用[^]代替点,因为JS' s与新行不匹配(如Matching multiline Patterns所示),但是这场比赛很贪婪并且也占用了很多,所以结果数组只有1个条目:
var split_pattern = /\b(\d\d?\/\d\d?\/\d\d\d\d)\n([^]+)/gm;
var array_of_mems = contents.match(split_pattern);
// => ["1/12/2011↵I did something else..."]
如果我添加一个问号来获取[^] + ?,根据How to make Regular expression into non-greedy?使得匹配非贪婪,那么我只得到内容部分的第一个字符。
最好的方法是什么?提前谢谢。
答案 0 :(得分:2)
(\d{1,2}\/\d{1,2}\/\d{4})\n|((?:(?!\n*\d{1,2}\/\d{1,2}\/\d{4})[\s\S])+)
你可以尝试这个抓住捕获。参见演示。
https://regex101.com/r/sJ9gM7/126
var re = /(\d{1,2}\/\d{1,2}\/\d{4})\n|((?:(?!\n*\d{1,2}\/\d{1,2}\/\d{4})[\s\S])+)/gim;
var str = '1/12/2011\nI did something.\n\n10/5/2013\nI did something else.\n\nHere is another line.\n\nAnd another.\n\n5/17/2014\nLalala.\nMore text on another line.';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
答案 1 :(得分:1)
您可以在循环中使用exec()
方法来获得所需的结果。
var re = /^([\d/]+)\s*((?:(?!\s*^[\d/]+)[\S\s])+)/gm,
matches = [];
while (m = re.exec(str)) {
matches.push(m[1]);
matches.push(m[2]);
}
<强>输出强>
[ '1/12/2011',
'I did something.',
'10/5/2013',
'I did something else.\n\nHere is another line.\n\nAnd another.',
'5/17/2014',
'Lalala.\nMore text on another line.' ]