1

I have a string that has sequential numbers such as the following:

1. this is some text 2. this is more text 3. this is even more text

I'd like to split this into an array that starts with the numbers:

['1. this is some text', '2. this is more text', '3. this is even more text']

I tried the following:

string.split(/(?=\d[.][ ])/g);

This works well when the numbers are 1-9. However, when the numbers >=10, it doesn't work well.

Any ideas on how to get this to work?

1 Answer 1

3

Yes, this is all you need to change (try on JSFiddle):

string.split(/(?=\b\d+[.] )/g);

The + after the \d tells it to match 1 or more.

EDIT: The \b is a word boundary to ensure you're getting the number from the beginning, not each separate digit.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work for me. 9 works fine, but it splits 10 like this: [...,'9. text goes here', '1', '0. the text for ten goes here', '1', '1. the next for eleven goes here']
@Marc: sorry about that, but the \b word boundary check I just added should do the trick.

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.