开发者

How to detect the url on webpage using javascript? [duplicate]

This question already has answers here: Detect URLs in text with JavaScript (15 answers) Closed 9 years ago.

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开发者_如何学JAVA 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜