Reputation: 948
I'm looking to use reg-ex to split the following string
1 hi my name is John. 2 I live at house 32. 3 I see stars.
to
[hi my name is John, I live at house 32. , I see stars]
Note that am trying to split on digit followed by a space
Upvotes: 4
Views: 33825
Reputation: 39550
Split on /(^|\b\s+)\d+\s+/g
.
Explanation:
(^|\b\s+)
A collection of either ^
or \b\s+
)
^
Start of the string OR\b\s+
a word boundary followed by a space/tab repeated 1 or more times\d+
A digit between 0 and 9 repeated 1 or more times (so it'd match 1, 12, 123, etc.)\s+
A space/tab repeated 1 or more timesEdit:
(^|\.\s+)\d+\s+
might work better for you.
Upvotes: 8
Reputation: 46430
Split on:
/\d+ /
Only the first match will be empty because it's the match just before the first number: 1
, so you gotta ignore that one.
Upvotes: 1