I found a regular expression that is suppose to capture URLs but it doesn’t capture some URLs.
$("#links").change(function() {
//var matches = new array();
var linksStr = $("#links").val();
var pattern = new RegExp("^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$","g");
var matches = linksStr.match(pattern);
for(var i = 0; i < matches.length; i++) {
alert(matches[i]);
}
})
It doesn’t capture this url (I need it to):
But it captures this
Several things:
The main reason it didn’t work, is when passing strings to
RegExp(), you need to slashify the slashes. So this:Should be:
Next, you said that FF reported, “Regular expression too complex”. This suggests that
linksStris several lines of URL candidates.Therefore, you also need to pass the
mflag toRegExp().The existing regex is blocking legitimate values, eg: “HTTP://STACKOVERFLOW.COM”. So, also use the
iflag withRegExp().Whitespace always creeps in, especially in multiline values. Use a leading
\s*and$.trim()to deal with it.Relative links, eg
/file/63075291/LlMlTL355-EN6-SU8S.rarare not allowed?Putting it all together (except for item 5), it becomes:
Which yields: