I'm trying to create a one line regex array value that splits on hyphens and underscores and also splits on (and remembers) groups of numbers (one or more).
I wrote a regex that achieves this, but it also inserts both empty strings and undefined
into the resulting array
https://jsfiddle.net/vdmom1qL/
var x = '232as-df98_rew_98_9fg9-dd988fff.jpg';
console.log(x.split(/(?:[-._])|(\d+)/));
//the output
//["", "232", "as", undefined, "df", "98", "", undefined, "rew", undefined, "", "98", "", undefined, "", "9", "fg", "9", "", undefined, "dd", "988", "fff", undefined, "jpg"]
What exactly am I doing wrong here? I mean the regex seems to be logically correct (from a readable standpoint it makes sense), but the empty strings and undefined
's are very strange
UPDATE
this is more about what is wrong with the regex then about just removing empty indexes from an array.
the main point I'm trying to drive home is: what is wrong with this regex that makes it create those empty strings and undefined
s?