Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Split a String Using Regular Expression in JavaScript</title> </head> <body> <script> let regex = /[\s,]+/; let str = "My favourite colors are red, green and blue"; let parts = str.split(regex); console.log(parts); // Loop through parts array and display substrings for(let part of parts){ document.write("<p>" + part + "</p>"); } </script> </body> </html>