I'm trying to get a relatively simple regex working the way I want it to. I'm trying to split a string into an array but ignoring blank lines. Here's what I've got so far
const regExp = /\s*(?:\n|$)\s*/;
const names = "\nBen\n\n\nLeah\nJosh\nJess";
console.log(names.split(regExp));
This is returning an array of
0: ""
1: "Ben"
2: "Leah"
3: "Josh"
4: "Jess"
As you can see all of the duplicated newlines are being correctly ignored but not if it's the first character. Can anyone suggest what amendment I need to make to get rid of that pesky blank first line.
names.trim().split(regExp)not an option?splitto cut around that region. This will always result in a leading empty string.Array.from(names.matchAll(/[^\s]+/g), matches => matches[0])works exactly as required. So, in addition to not knowing why not just trim, I also don't know why this should be a split.names.match(/\S(?:.*\S)?/g). Or.split(/[\r\n]/).map(l => l.trim()).filter(Boolean)