Combining two indexOf and regex in to one I have the following code that splits a combination of names on either the word and or the &amp; ampersand:

    var name = 'Ron &amp; Peggy Sue'; //or 'Ron and Peggy Sue';

    if ( name.indexOf('&amp;') &gt; -1 ){
        names = name.split(/ &amp; /g);
    }

    if ( name.toLowerCase().indexOf(' and ') &gt; -1 ) {
        names = name.split(/ and /gi);
    }


Its returns an array of names like:

names = ['Ron', 'Peggy Sue']; //regardless of `&amp;` 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?
You were almost there. You can use pipe (a|b) to designate the regex to look for both a and b. With that knowledge your split is as easy as:

var name = 'Ron &amp; Peggy Sue and Darin Douglass';
names = name.split(/\s(?:and|&amp;)\s/ig);


EDIT: The syntax (?:...) is whats called non-capturing parens. These match the regex provided as '...' but it does not remember the matches. The problem with the simple /\s(and|&amp;)\s/ig/ was that the words and and &amp; would be matched. This new syntax 'matches' them but does not remember (i.e. report) them. Thanks to @cbojar for the pointer.

Also, there is no need to check for either delimiter in name. If none exist it will simply return a single-element array with name as its element.