I have the following code that splits a combination of names on either the word and
or the &
ampersand:
var name = 'Ron & Peggy Sue'; //or 'Ron and Peggy Sue';
if ( name.indexOf('&') > -1 ){
names = name.split(/ & /g);
}
if ( name.toLowerCase().indexOf(' and ') > -1 ) {
names = name.split(/ and /gi);
}
Its returns an array of names like:
names = ['Ron', 'Peggy Sue']; //regardless of `&` or `and` separator
I was trying to combine these two conditions into one, but the results were not what I wanted. I know there must be a better way. How can the above code be improved?