Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat array values

I have this string :

Item 1Item 2Item 3Item 4

I want to get :

["Item 1", "Item 2", "Item 3", "Item 4"]

I tried to do this :

var string = 'Item 1Item 2Item 3Item 4'
var regex = string.split(/(\d)/)
regex.splice(-1, 1)
regex[regex.length - 2] += regex[regex.length - 1];
regex.splice(-1, 1)
console.log(regex);

But it doesn't work, any idea how to get the desired result?

EDIT :

The string at max could look like this :

Item 1Item 2Item 3Item 4Item NItem N-1
like image 219
Alexandre Elshobokshy Avatar asked Dec 14 '25 08:12

Alexandre Elshobokshy


1 Answers

Note: the answer handles the original case - before the update

Use String.match() to find a sequence of non numbers that ends with a sequence of numbers (regex101):

var string = 'Item 1Item 2Item 3Item 4'
var arr = string.match(/\D+\d+/g)
console.log(arr);
like image 122
Ori Drori Avatar answered Dec 15 '25 22:12

Ori Drori