BigPoppa
BigPoppa

Reputation: 1215

Javascript URL Regex Issue in NodeJS

So I need to grab all urls in a string and return them in an array.

Here is what I have so far:

        var comment = 'Check out www.google.com and http://bing.com';
        var regexp = new RegExp('((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?', 'gi');
        var urls = comment.match(regexp);

Right now its not returning anything, whereas I need it to return both urls. I have searched google and stack and lots of examples, but none are working for my needs.

All help is greatly appreciated.

Thanks!

Jim

Upvotes: 0

Views: 650

Answers (1)

SLaks
SLaks

Reputation: 887479

Your \ characters are being parsed as string escapes and aren't ending up in the regex.

Instead, you should use a regex literal:

/((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi

Upvotes: 3

Related Questions