最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - JavaScript Split without losing character - Stack Overflow

programmeradmin7浏览0评论

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

9:30 pm
The user did action A.

10:30 pm
Wele, user John Doe.

11:30 am
Messaged user John Doe

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

9:30 pm
The user did action A.

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.

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

Thanks!

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

9:30 pm
The user did action A.

10:30 pm
Wele, user John Doe.

11:30 am
Messaged user John Doe

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

9:30 pm
The user did action A.

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.

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

Thanks!

Share Improve this question edited Jun 15, 2010 at 17:22 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Jun 15, 2010 at 16:39 RohanRohan 3,8054 gold badges23 silver badges16 bronze badges 2
  • Any reason that you don't want to split on double newline? – BalusC Commented Jun 15, 2010 at 16:43
  • Yep, please see the reply to Vivin's answer. – Rohan Commented Jun 15, 2010 at 16:48
Add a ment  | 

2 Answers 2

Reset to default 6

Use a lookahead:

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

Wouldn't it be easier to split on the newline?

var split = journals.split(/\n\n/);

EDIT

Try normalizing the string into a format that you can use:

/*
 Non-normalized string
*/
var str = "9:30 pm\nThe user did action A.10:30 pm\nWele, 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);
}
发布评论

评论列表(0)

  1. 暂无评论