How to split a camel case string containing numbers

I have camel cased strings like this:

"numberOf40"
"numberOf40hc"

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"

a.split(/(?=[A-Z0-9])/)
// ["number", "Of", "4", "0hc"]

a.split(/(?=[A-Z])|[^0-9](?=[0-9])/)
// ["number", "O", "40hc"]

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

Answer

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;

[
  "numberOf40",
  "numberOf40hc"
].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;

[
  "numberOf40",
  "numberOf40hc"
].forEach(s => console.log(s.split(regex)));