0

I have a HTML file which has URLs in the text. I want to filter URLs like

See http://google.com
See the stackover.com

I want output like:

http://google.com
stackover.com
mplungjan
  • 169,008
  • 28
  • 173
  • 236
ajai
  • 187
  • 1
  • 2
  • 15

2 Answers2

0

Are you looking for this?

$('a[href^=http://]').each(function() {
    var href = $(this).attr('href');
    $(this).text(href);
});
D4V1D
  • 5,805
  • 3
  • 30
  • 65
0

HTML

<p>http://www.linkedin.com</p>
<div>http://www.twitter.com</div>
<p><a href="http://google.net" />Google</a></p>
<a href="http://www.google.com" />Google</a>
<a href="http://google.net" />Google</a>
<a href="www.google.com" />Google</a>
<a href="http://www.google.org" />Google</a>
<a href="http://www.google.cc" />Google</a>
<a href="http://google.in" />Google</a>
<a href="http://google.edu" />Google</a>

JavaScript

$(function(){
  var linkifiedBody = linkify($('body').text());
  linkifiedBody= $('<div></div>').html(linkifiedBody);
  var links = linkifiedBody.find("a[href]");
  links = links.add($("a[href]"));
  links.each(function(){
  var hrefVal = $(this).attr('href');
if(isUrl(hrefVal))
    console.log(hrefVal);
  });
});


function isUrl(s) {
var regexp = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i;
return regexp.test(s);
}

function linkify(html) {
  return html.replace(/[^\"]http(.*)\.([a-zA-Z]*)/g, ' <a href="http$1.$2">http$1.$2</a>');
}

Demo

Narendra
  • 431
  • 1
  • 5
  • 19
  • It doesn't work. I unable to filter the url links while the HTML file have markup tags between the text. Below I have placed my codings for your reference. – ajai May 14 '15 at 05:12
  • $(document).ready(function() { $("input#url").click(function() { var messagea = $("#message").val(); var href = $(messagea).filter("a[href*='http://']"); $("
    ").appendTo(href); $("#result").html(href); }); });
    – ajai May 14 '15 at 05:14
  • Above example, the code doesn't filter the URL links only. I want to filter the URL links it may appear between the anchor tag or text. Below I have placed my code for your reference. It works while doesn't have HTML markup tags in HTML except the anchor tag. – ajai May 14 '15 at 05:52
  • HTML:
    jQuery: $(document).ready(function() { $("input#url").click(function() { var messagea = $("#message").val(); var href = $(messagea).filter("a[href*='http://']"); $("
    ").appendTo(href); $("#result").html(href); }); });
    – ajai May 14 '15 at 05:52
  • i have updated my answer and the demo link. however, im still not sure if i understood ur requirement completely. please check if it suffice ur needs. – Narendra May 14 '15 at 12:12