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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With