Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split line via regex in javascript?

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

enter image description here

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.

like image 632
Royi Namir Avatar asked Jul 16 '14 11:07

Royi Namir


People also ask

Can I use regex in Split in JavaScript?

You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.

How do you split a line in JavaScript?

Splitting a String on the Newline Character You can split a long string into its individual lines when seeing a line break. A line break uses the “newline” character. The character to represent a new line in JavaScript is the same as in other languages: \n .

Can you split with regex?

Split by regex: re. If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.

How do you split a string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

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
like image 144
anubhava Avatar answered Oct 14 '22 12:10

anubhava