0

I am working with this Java Script and am trying to figure out how to include the hyphen all the way to the end of the link.

Here is the sample link: http://www.mydomain.com:5678/cgi-bin/encoder?USER=admin&PWD=123456&VIDEO_RESOLUTION=N1280x720

And here is the code ((http://jsfiddle.net/moe3615/G9A86/)

// Check the main container is ready
$('#feed').ready(function(){
// Get each div
$('.content').each(function(){
// Get the content
var str = $(this).html();
// Set the regex string
var regex = /(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/ig
// Replace plain text links by hyperlinks
var replaced_text = str.replace(regex, "<a href='$1' target='_blank'>$1</a>");
// Echo link
$(this).html(replaced_text);
});
});

Any help would be greatly appreciated

Moe
  • 77
  • 1
  • 7
  • There is a legendary post on why using regex to parse HTML is both difficult and generally a bad idea. That said, can you explain what hyphen you are talking about? I don't see it. – BlackVegetable Jul 03 '14 at 16:57
  • "*how to include the hyphen all the way to the end of the link.*" — What do you mean? What is the problem that you're trying to solve? What is the current result, and what is the expected result? – Amal Murali Jul 03 '14 at 16:57
  • In the link that I have it has a hyphen at cgi-bin right after the domain name. – Moe Jul 03 '14 at 16:58
  • If you see the jsfiddle link, it only creates a link until the hyphen anything after that is not included in the link – Moe Jul 03 '14 at 16:59
  • Oh, you meant how to include **from** the hyphen to the end of the link! – BlackVegetable Jul 03 '14 at 17:00
  • So you're trying to take a plaintext "link" and turn it into an actual html link? – Marc B Jul 03 '14 at 17:00
  • @user3802591: It's still unclear. Anyway, see if this helps: [How to replace plain URLs with links?](http://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links) – Amal Murali Jul 03 '14 at 17:02
  • Yes, precisely that. I have a page with hundreds of links that look similar to the link above, I want to turn them into a link. – Moe Jul 03 '14 at 17:03
  • `/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.-]*(\?\S+)?)?)?)/ig` just add the hyphen to the list of allowed characters, no? – Jakub Kotrs Jul 03 '14 at 17:03
  • Jakub for some reason when I do that if there is a at the end of a link it includes that as well and it breaks the link. – Moe Jul 03 '14 at 17:07
  • You may need to escape the hyphen? – BlackVegetable Jul 03 '14 at 17:15
  • What do you mean escape the hyphen? – Moe Jul 03 '14 at 17:23
  • @JakubMichálek Is there a way to add non allowed characters? Perhaps if I eliminate the < it won't include that in the link...? I wish I knew more about Jquery... trying to learn. Thanks for your help – Moe Jul 03 '14 at 17:30

0 Answers0