I have a regex expression for finding url's in a string, but when I use it with String.prototype.split() it returns undefined's.
const regex = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
const str = "yay http://www.google.com woo www.google.com haha google.com";
console.log(str.match(regex));
// [ 'http://www.google.com', 'www.google.com', 'google.com' ]
console.log(str.split(regex));
// [ 'yay ','http://w',undefined,undefined,'',' woo ',undefined,undefined,'www.','',' haha ',undefined,undefined,undefined,'','' ]
After some research it appears that this has to do with capturing groups. I attempted adding :? to all the capturing groups (parts wrapped in parenthesis) and it removed the undefined's.
const reg2 = /(?:http(?:s)?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=]*)/g
const str = "yay http://www.google.com woo www.google.com haha google.com";
console.log(str.split(reg2));
// [ 'yay ', ' woo ', ' haha ', '' ]
But it is omitting the urls. I am looking to return:
[ 'yay ', 'http://www.google.com', ' woo ', 'www.google.com', ' haha ', 'google.com' ]