How to split a camel case string containing numbers

I have camel cased strings like this:


How can I split it like this?

["number", "Of", "40"]
["number", "Of", "40hc"]

I am using humps to decamelize keys so I can only pass a split regex as option. I am looking for an answer that only uses split function.

My best attempts:

const a = "numberOf40hc"

// ["number", "Of", "4", "0hc"]

// ["number", "O", "40hc"]

Also I don’t understand why the f is omitted in my last attempt.


You don’t get the f in you last attempt (?=[A-Z])|[^0-9](?=[0-9]) as this part of the last pattern [^0-9] matches a single char other than a digit and will split on that char.

You could also match instead of split

const regex = /[A-Z]?[a-z]+|d+[a-z]*/g;

].forEach(s => console.log(Array.from(s.matchAll(regex), m => m[0])));

Using split only, you could use lookarounds with a lookbehind which is gaining more browser support.

const regex = /(?=[A-Z])|(?<=[a-z])(?=d)/g;

].forEach(s => console.log(s.split(regex)));