Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex split on upper case and first digit

I need to split the string "thisIs12MyString" to an array looking like [ "this", "Is", "12", "My", "String" ]

I've got so far as to "thisIs12MyString".split(/(?=[A-Z0-9])/) but it splits on each digit and gives the array [ "this", "Is", "1", "2", "My", "String" ]

So in words I need to split the string on upper case letter and digits that does not have an another digit in front of it.

like image 675
Jesper Palm Avatar asked Dec 16 '22 03:12

Jesper Palm


2 Answers

Are you looking for this?

"thisIs12MyString".match(/[A-Z]?[a-z]+|[0-9]+/g)

returns

["this", "Is", "12", "My", "String"]
like image 182
georg Avatar answered Dec 29 '22 16:12

georg


As I said in my comment, my approach would be to insert a special character before each sequence of digits first, as a marker:

"thisIs12MyString".replace(/\d+/g, '~$&').split(/(?=[A-Z])|~/)

where ~ could be any other character, preferably a non-printable one (e.g. a control character), as it is unlikely to appear "naturally" in a string.

In that case, you could even insert the marker before each capital letter as well, and omit the lookahead, making the split very easy:

"thisIs12MyString".replace(/\d+|[A-Z]/g, '~$&').split('~')

It might or might not perform better.

like image 39
Felix Kling Avatar answered Dec 29 '22 16:12

Felix Kling