javascriptregex

How can I split string by comma and/or new line and/or whitespace


Hi there I am searching for regex to split emails, but no success so far. What is the point I want to make possible to separate this:

o@gmail.com b@gmail.com c@gmail.om

or

o@gmail.com, b@gmail.com,c@gmail.com

or

o@gmail.com,
 b@gmail.com, c@gmail.com

Solution

  • You need to split on either a comma surrounded by some number of white-space characters, or just one or more white-space characters:

    let strs = ['o@gmail.com b@gmail.com c@gmail.om',
    'o@gmail.com, b@gmail.com,c@gmail.com',
    'o@gmail.com,\n\
     b@gmail.com, c@gmail.com'];
    
    console.log(strs.map(s => s.split(/\s*,\s*|\s+/)))