Basically I want to do this:
I get a string "myVariableName"
and turn it into: ["my", "variable", "name"]
I tried doing this using regex, but I get a lot of undefineds in my ending array it seems. The two possible cases for variable names are camel-case and upper snake-case in this situation.
const matchVariableNames = /(\b[a-z]+)|([A-Z][a-z]+)|(\b[A-Z]+)|(_[A-Z]+)/g;
const variableName = 'myVariable';
let words = [];
let regexMatches;
while (regexMatches = matchVariableNames.exec(variableName)) {
regexMatches.forEach((match) => {
words.push(match);
});
};
outputs:
["my", "my", undefined, undefined, undefined, "Variable", undefined, "Variable", undefined, undefined]
undefined