0

I am writing a application using JQuery in which I included my script in the head section of the webpage and the data of the page is dynamic and comes from the database.

Now I need to detect the URLs on the page. It can be an anchor tag or a simply a written URL.

For detecting an URL I am using this function

function checkURL(value) {
  var urlregex = new RegExp(
        "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){0,1}([0-9A-Za-z]+\.)");
  if(urlregex.test(value))
  {
    return(true);
  }
  return(false);
}

It works fine but if a URL doesn't have a www prefix then it fails e.g. myweb.com

Please suggest a better regex for my code which works fine with all valid URLs including sub domains.

Can you also suggest a best way to detect URLs on my webpage?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Peeyush
  • 4,728
  • 16
  • 64
  • 92
  • thanks every one for edit and thansk Felix Kling for the use full link but still the regex problem not solved – Peeyush Sep 21 '11 at 09:27
  • i just need a regex which can validate almost every url with or without www. prefix and also validate sub domains i do not want to validate ftp url's only simple general web urls – Peeyush Sep 21 '11 at 09:29

1 Answers1

0

All hyperlinks will be starting with any of , or etc. Considering this, just call document.getElementsByTagName() to an array. The array will hold all the link contents. Just extract the hyperlink, either direct or from attributes of the array object.

Kris
  • 8,680
  • 4
  • 39
  • 67