Nasir
Nasir

Reputation: 4865

How do I get this JQuery to target & shorten URL's in a string?

I'm just starting out with JQuery.

My Question: I want to know if I can modify this code below to shorten long URLs, but ignore anything that isn't a URL?

My Problem: This code fragment targets everything in the DIV ('tasks-overflow'). Although I just need it to affect URL's in that DIV based on protocol(http://...). There are sentences on either side of the URL.

$(document).ready(function(){
  $(".tasks-overflow:contains('http://')").each(function(){
    $(this).text("http://...");
  });
});

Any help would greatly be appreciated, thanks

Upvotes: 1

Views: 153

Answers (1)

BGerrissen
BGerrissen

Reputation: 21680

Changed my awnser after I read your question better. Try this:

$(document).ready(function(){
  $(".tasks-overflow:contains('http://')").each(function(){
    var self = $(this)
    , txt = self.text();
    txt = txt.replace(/(http[s]*:[\/\\]{2})[^\s]*/g,'$1...');
    self.text(txt);
  });
});

Upvotes: 2

Related Questions