I've got a regex.exec() function like this:
var chat = reader.result;
var regex = /(\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}) (\d\d:\d\d:\d\d): ([^:]+): (.*)/g;
var messages = [];
var match;
while( match = regex.exec(chat)) {
messages.push({
date: match[1],
time: match[2],
name: match[3],
message: match[4]
});
}
match[1] seems to work on almost all dates: dd/mm/yyyy to dd-mm-yy
However, when the input is in the following format:
22-2-2014 18:37:15: Andre: Moet nog 2,5 aflevering
it outputs an empty array messages
I don't really know where this is coming from but I believe it's regex match[1]
, because when the input is 28/02/14 00:03:03: Tom: Je gaat nu de afweging maken
It returns an array of objects just fine.
Ps for the ones interested, the reader.result is from the HTML5 filereader API.
EDIT I've updated the question with a bug in the regex that was found by a commenter. Still, even with the newer regex the input still fails
Looks like your regex misses the exception for a single digit month. try this:
/(\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}) (\d\d:\d\d:\d\d): ([^:]+): (.*)/g
edit: also built in optional single digit days.
Try this regex ( see demo link )
(\d\d[\/-]\d\d?[\/-]\d{2,4}) (\d\d:\d\d:\d\d): ([^:]+): (.*)
it matches both:
22-2-2014 18:37:15: Andre: Moet nog 2,5 aflevering
22-12-2014 18:37:15: Andre: Moet nog 2,5 aflevering
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.