2

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' ]

1 Answer 1

2

You might be able to just split on whitespace here:

var str = "yay http://www.google.com woo www.google.com haha google.com";
var parts = str.split(/\s+/);
console.log(parts);

If the leading/trailing whitespace are really required here, then try matching on the pattern:

<URL>|\s*\S+\s*

This would match either a URL, or a series of non word characters, with surrounding whitespace. Consider:

var str = "yay http://www.google.com woo www.google.com haha google.com";
console.log(str.match(/(?:http(s)?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=]*)|\s*\S+\s*/g));

This uses an alternation trick to first try to selectively consume a URL, with no surrounding whitespace. That failing, the fallback pattern is \s*\S\s*, i.e. any other word with the leading/trailing whitespace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.