JavaScript拆分而不会丢失字符 [英] JavaScript Split without losing character

查看:162
本文介绍了JavaScript拆分而不会丢失字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript拆分某些文本。文字如下:

I want to split certain text using JavaScript. The text looks like:

9:30 pm
The user did action A.

10:30 pm
Welcome, user John Doe.

11:30 am
Messaged user John Doe

现在,我想把字符串拆分成事件。 ie:

Now, I want to split the string into events. i.e.:

9:30 pm
The user did action A.

将是一个事件。我正在使用RegEx:

would be one event. I'm using RegEx for this:

var split = journals.split(/\d*\d:/);

事实上,前两个字符正在丢失。拆分如下所示:

Thing is, the first two characters are getting lost. The split appears like this:

30 pm
    The user did action A.

如何分割以使分割保持前两个/三个字符(即 9: 10:)等等?

How do I split so that the split maintains the first two/three characters (ie 9: or 10:) etc?

谢谢!

推荐答案

使用前瞻:

var split = journals.split(/(?=\b\d+:)/);

这篇关于JavaScript拆分而不会丢失字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆