Some regular expression help?

Posted by Rohan on Stack Overflow See other posts from Stack Overflow or by Rohan
Published on 2010-06-18T07:37:29Z Indexed on 2010/06/18 7:43 UTC
Read the original article Hit count: 362

Filed under:
|

Hey there. I'm trying to create a Regex javascript split, but I'm totally stuck. Here's my input:

9:30 pm
The user did action A.

10:30 pm
Welcome, user John Doe.

***This is a comment

11:30 am
This is some more input.

I want the output array after the split() to be (I've removed the \n for readability):

["9:30 pm The user did action A.", "10:30 pm Welcome, user John Doe.", "***This is a comment", "11:30 am This is some more input." ];

My current regular expression is:

var split = text.split(/\s*(?=(\b\d+:\d+|\*\*\*))/);

This works, but there is one problem: the timestamps get repeated in extra elements. So I get:

["9:30", "9:30 pm The user did action A.", "10:30",  "10:30 pm Welcome, user John Doe.", "***This is a comment", "11:30", "11:30 am This is some more input." ];

I cant split on the newlines \n because they aren't consistent, and sometimes there may be no newlines at all.

Could you help me out with a Regex for this?

Thanks so much!!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about split