Here are a few key points to consider:
The main issue with your code not working is that when passing strings to RegExp()
, you need to double up on the slashes. So instead of this:
"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"
You should use:
"^(https?:\/\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\/\\w \\.-]*)*\/?$"
You mentioned that Firefox reported "Regular expression too complex." This indicates that your linksStr
likely contains multiple lines of URL candidates.
Therefore, make sure to include the m
flag when calling RegExp()
.
Your current regex pattern may be blocking valid URLs like "HTTP://STACKOVERFLOW.COM". To allow for case insensitivity, include the i
flag in your RegExp()
call.
Deal with potential whitespace by using \s*
at the beginning and utilizing $.trim()
.
Are relative links such as
/file/63075291/LlMlTL355-EN6-SU8S.rar
permitted?
Combining all these adjustments (excluding item 5), your updated code would look like this:
var linksStr = "http://www.wupload.com/file/63075291/LlMlTL355-EN6-SU8S.rar \n"
+ " http://XXXupload.co.uk/fun.exe \n "
+ " WWW.Yupload.mil ";
var pattern = new RegExp (
"^\\s*(https?:\/\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\/\\w \\.-]*)*\/?$"
, "img"
);
var matches = linksStr.match(pattern);
for (var J = 0, L = matches.length; J < L; J++) {
console.log ( $.trim (matches[J]) );
}
When run, this will output the following list of URLs:
http://www.wupload.com/file/63075291/LlMlTL355-EN6-SU8S.rar
http://XXXupload.co.uk/fun.exe
WWW.Yupload.mil