split line via regex in javascript?

javascript, regex

Solution

You can use this regex:

/^(\d+(?:\.\d+)*) (\w+(?: \w+)*)/gm

And get your desired matches using matched group #1 and matched group #2.

Online Regex Demo

Update: For `String#split` you can use this regex:

/ +(?=[A-Z\d])/g

Regex Demo

Update 2: With the possibility of having capital letters also in chapter names following more complex regex is needed:

var re = /(\D +(?=[a-z]))| +(?=[a-z\d])/gmi; 
var str = '1.6.3 Type Foo Bar........................................................ 13';
var m = str.split( re );
console.log(m[0], ',', m.slice(1, -1).join(''), ',', m.pop() );

//=> 1.6.3 , Type Foo Bar........................................................ , 13

Problem

I have this structure of text : ``` 1.6.1 Members................................................................ 12 1.6.2 Accessibility.......................................................... 13 1.6.3 Type parameters........................................................ 13 1.6.4 The T generic type aka <T>............................................. 13 ``` I need to create JS objects : ``` { num:"1.6.1", txt:"Members" }, { num:"1.6.2", txt:"Accessibility" } ... ``` That's not a problem. The problem is that I want to extract values via Regex split via positive lookahead : Split via the first time you see that next character is a letter What have i tried : ``` '1.6.1 Members........... 12'.split(/\s(?=(?:[\w\. ])+$)/i) ``` This is working fine : ``` ["1.6.1", "Members...........", "12"] // I don't care about the 12. ``` But If I have 2 words or more : ``` '1.6.3 Type parameters................ 13'.split(/\s(?=(?:[\w\. ])+$)/i) ``` The result is : `["1.6.3", "Type", "parameters................", "13"]` //again I don't care about 13. Of course I can join them , but I want the words to be together. Question : How can I enhance my regex NOT to split words ? Desired result : `["1.6.3", "Type parameters"]` or `["1.6.3", "Type parameters........"]` // I will remove extras later or `["1.6.3", "Type parameters........13"]`// I will remove extras later NB I know I can do split via " " or by other simpler solution but I'm seeking ( for pure knowledge) for an enhancement for my solution which uses positive lookahead split. Full online example : nb2 : The text can contain capital letter in the middle also.

Original source