Jump to content

replace string with a single link, nothing else.


shortysbest

Recommended Posts

what i'm attempting to do is to build a video embedding system into my comment stream, and I have all of that working for the most part,but what I am trying to do now is to have the link preview before you post the status, I just want it to allow 1 link to pass through to be posted to the php preview page.

 

so if you type in the textbox something like "check this video out 

then I just want it to get the video link and nothing else, and I don't want it to allow double links, like if someone posts:

"check this video out

 

i just want it to return the first link once.

 

This is the code i'm using to change the text into a link, right now it shows the video and everything but it just shows the other text, and double links.

 

 

var comment = encodeURIComponent($("#comment").val());

  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  var ab = comment.replace(exp,"<a href='$1'>$1</a>"); 

 

 

This works but I'd like to see the solution that uses replace.

 

var message = "check this video out http://www.youtube.com/watch?v=3kBbun8xdDQ http://www.youtube.com/watch?v=3kBbun8xdDQ";
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  var m = message.match(exp);
  var link = '<a href="' + m[0] + '">' + m[0] + '</a>';
  alert(link);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.