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?

2
  • thanks every one for edit and thansk Felix Kling for the use full link but still the regex problem not solved Commented Sep 21, 2011 at 9: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 Commented Sep 21, 2011 at 9:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.