Trying to get a regex to recognize and extract words from both camelCase and CamelCase

I’ve got this halfway working. This works great:

'MyOwnVar'.match(/([a-z]*)([A-Z][a-z]+)/g)

Result:

["My", "Own", "Var"]

The goal is to pull out the individual words. But if I pass a camelCase name to it:

'myOwnVar'.match(/([a-z]*)([A-Z][a-z]+)/g)

I get:

["myOwn", "Var"]

I can’t figure out what I’m doing wrong. As far as I can tell, two sets of () should store the matching results in two separate array elements. It’s lumping them in together for some reason.


Source: regex

Leave a Reply