我正在尝试创建一个正则表达式的javascript分割,但是我完全卡住了。这是我的输入:
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.
我希望在split()函数之后得到输出数组(为了方便阅读,我已经去掉了
\n
):["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." ];
我目前使用的正则表达式是:
var split = text.split(/\s*(?=(\b\d+:\d+|\*\*\*))/);
这个方法是有效的,但有一个问题:时间戳会在额外的元素中重复出现。所以我得到了:
["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." ];
我无法按照换行符\n
进行分割,因为它们不一致,有时根本没有换行符。
你能帮我写一个正则表达式吗?
非常感谢!
编辑:回复phleet的评论
可能会像这样:
9:30 pm
The user did action A.
He also did action B
10:30 pm Welcome, user John Doe.
基本上,时间戳后可能会有换行符,事件描述可能会有多个换行符。