1

I just asked a question about Regex, and received a great answer: JavaScript Split without losing character

Now, I have another question.

My current Regex looks like this:

var split = text.split(/(?=\w*\d*\d:\d\d)/);

Basically, I'm trying to split using the timestamps (eg - 9:30 or 10:30, the difference between them is the extra digit in the latter). How do I go about this?

Currently, if I have these two:

9:30 pm
The user did action A.

10:30 pm
Welcome, user John Doe.

The splits are :

9:30 pm
The user did action A.
----
1
----
0:30 pm
Welcome, user John Doe.

How do I add an optional check for the first character in the timestamp?

Thanks!

1

3 Answers 3

1

See my answer to your other question, I fixed this problem in the regex by adding a word boundary:

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

Updated it with \s* to strip out any unnecessary whitespace (but not the line breaks) too. Result:

["9:30 pm    
The user did action A.", "10:30 pm  
Welcome, user John Doe.", "11:30 am
Messaged user John Doe"]
Sign up to request clarification or add additional context in comments.

Comments

1
var split = text.split(/(?=\w*[\d]{1,2}:[\d]{2})/);

RegexPal is helpful for these tasks.

1 Comment

Which doesn't -- that pattern or the site? If you are using the site, you need to just put in the pattern, i.e. (\w*\d*\d:\d\d) will match the input you gave.
1

I'm not clear on what you're trying to do to the text, but I do have a regex that hopefully can help match the times only.

\d{1,2}:\d{1,2} (am|pm)

The problem with your regex and andy's regex is that the * is greedy. It means zero or more matches, as many times as possible. Using {min,max} with the exact numbers you need will be more accurate and avoid the greedy *.

edit: Andy's does in fact work on that site he linked. And the * doesn't seem to be greedy. Does either pattern work for you?

1 Comment

{min, max} was the first thing I tried. The real problem is the lookahead, it will look ahead and find 0:30 and split on that, instead of 10:30. Adding a required word boundary forces the lookahead to find where the "word" begins. +1 and welcome to Stack Overflow :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.