Parse for Links with Prototype JS

2 Comments
Tags: , , , , , ,
Posted 25 Mar 2009 in Programming

Parsing for links with the Prototype javascript library is easy. Here is the pattern for finding links

/(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^
=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/

And to implement it you can loop through your containers that might contain links

document.observe("dom:loaded", function(){
var posts = $$("div#posts");
for(var i = 0; i < posts.length; i++){
var link_regex = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^
=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/;
var parsed_string = posts[i].innerHTML.gsub(link_regex, '<a href="#{0}"
target="_blank">#{0}</a>');
posts[i].innerHTML = parsed_string;
}
});

Related posts:

  1. Accessing Links in Nested TD Cells with Prototype
  2. Absolutize Relative Links Using PHP and Preg_Replace_Callback
  3. Using Prototype to Access Form Data
  4. Using jQuery and Prototype Javascript Together with jQuery.noConflict();
  5. Rails Prototype JS and TinyMCE Autosave

2 Comments

  1. mike

    you’d write that code and yet you have an issue w/ Perl? you’re nuts!

  2. perl is dead… long live ruby!!! but this is javascript/prototype library anyway. but how would you write this in perl?



Add Your Comment