我想使用JavaScript拆分某些文本。文字如下:
9:30 pm
The user did action A.
10:30 pm
Welcome, user John Doe.
11:30 am
Messaged user John Doe
现在,我想将字符串拆分为事件。即:
9:30 pm
The user did action A.
将是一个事件。我正在使用RegEx:
var split = journals.split(/\d*\d:/);
事情是,前两个字符迷路了。分裂看起来像这样:
30 pm
The user did action A.
如何分割以使分割保持前两个/三个字符(即 9:或 10:)等?
谢谢!
答案 0 :(得分:6)
使用前瞻:
var split = journals.split(/(?=\b\d+:)/);
答案 1 :(得分:2)
拆分换行符会不会更容易?
var split = journals.split(/\n\n/);
修改强>
尝试将字符串规范化为您可以使用的格式:
/*
Non-normalized string
*/
var str = "9:30 pm\nThe user did action A.10:30 pm\nWelcome, user John Doe.\n\n\n11:30 am\nMessaged user John Doe\n12:30 pm\nThe user did something else.";
/*
Normalizing into a specific format. TIMESTAMP\nDESCRIPTION\n\n.
Then removing extraneous leading \n\n
*/
str = str.replace(/\n*([0-9]{1,2}:[0-9]{2} (a|p)m)\n*/g, "\n\n$1\n").replace(/^\n+/, "");
var events = str.split(/\n\n/);
/*
The following should display an array of strings of the form:
TIMESTAMP\nDESCRIPTION
*/
console.log(events);
/*
Loop through events and split on single newline to get timestamp and description
*/
for(var i = 0; i < events.length; i++) {
var event = events[i];
var eventData = event.split(/\n/);
var time = eventData[0];
var description = eventData[1];
console.log(time, description);
}