Reputation: 461
I want to split names into array with these kind of string.
Bol BolLouis King
Brandon ClarkeRui Hachimura
Michael Jeffery JordanDennis Rodman
to
['Bol Bol', 'Louis King']
['Brandon Clarke', 'Rui Hachimura']
['Michael Jeffery Jordan', 'Dennis Rodman']
I have already retied creating my own regex using ^[A-Z]\w+\s[A-Z][a-z]+
but this only matches the first name and i can't capture the 2nd or 3rd name. I'm also having some issues when the name has 3 words on it like Michael Jeffery Jordan
Upvotes: 0
Views: 65
Reputation: 91438
As not all browsers support lookbehind, here is a solution without:
var test = [
'Bol BolLouis King',
'Brandon ClarkeRui Hachimura',
'Michael Jeffery JordanDennis Rodman',
];
console.log(test.map(function (a) {
// return a + ' :' + a.match(/\b[a-z]{1,2}\K\s/);
return a.match(/^(.+?[a-z])([A-Z].+)/);
}));
Upvotes: 0
Reputation: 1150
You didn't say where you want this so here is a sed version, which works for your sample input:
sed -e "s/\(.*[a-z]\)\([A-Z].*\)/['\1', '\2']/g"
Upvotes: 0
Reputation: 99
I would suggest making use of a positive lookahead to be able to generalize your pattern. That allows you to match an expression that is immediately followed by some other expression. Use a (?=someRegexp) at the end of your pattern to make the end be the case where a lowercase character is immediately followed by an uppercase one. You can then generalize to any number of words.
I would also suggest splitting it into two cases then, as the last name in your expression wouldn’t be followed by a capital letter but rather by a end of string character. You can do that with an or: (someRegexp|someOtherRegexp)
Upvotes: 1