Possible Duplicate:
Check if a Javascript string is an url
I am not very familiar with RegEx so it would be great if you could help me solve this problem.
I need a JS function that will:
- Check for the string (textarea input) if it has any url.
- If yes - search for the first url found and return array(true,url)
- If no - return array(false,"")
The problem I am having is this pattern that validates input.
function _is_url(url) {
var pattern = /(((ht|f)tps?\:\/\/)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5})(:[\d]{1,5})?))\S+/i;
var result = new Array();
var testing = pattern.test(url);
if(testing) {
var newurl = url.match(pattern);
result[0] = true;
result[1] = newurl[0];
console.log(result[1]);
}
else {
result[0] = false;
result[1] = "";
}
return result;
}
With this pattern, testing == true even if the url is http://www.youtube which is not a valid url.
What I need is to check the full url: http://www.youtube.com and any other url.