I need the regex for all non alphabetic character and capital letters only.
let str = "ThisIs-an_example";
What regex should I use for separating the words correctly using arr.join so that it is "this is an example"
Something like this?
let str = "ThisIs-an_example";
let result = str.replace(/([A-Z])/g, ' $1').toLowerCase().trim().replace(/[^a-z]/g, ' ');
Not quite what you asked for (array), but does the job.
You've got a couple of issues. First is that split doesn't include the separator, so if you just split on any upper case or non-letter character, then you'll end up losing the uppercased letters. (see first example).
So you can split on non-alpha characters OR a zero width assertion of an upper case character following something (see second example), but then you have an issue with the casing in the final string. You'll have to map back if you want to deal with that (see third example).
let str = "ThisIs-an_example";
let arr = str.split(/[^a-z]/);
console.log(arr);
arr = str.split(/[^A-Za-z]|(?=[A-Z])/);
console.log(arr.join(" "));
let cased = arr.map((a,i)=>{return i>0 ? a.toLowerCase() : a});
console.log(cased.join(" "));
Also note that this is going to split on any numbers that are included. If you expect numbers to ever appear, you will have to modify the expression.
You can split on a non-alphabetic character or a lookahead for an uppercase letter.
let str = "ThisIs-an_example";
let parts = str.split(/(?=[A-Z])|[^a-z]/);
console.log(parts);
console.log(parts.join(' ').toLowerCase());