Parse for Links with Prototype JS

       

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;
}
});

Tagged w/ #interface #javascript #js #parse #prototype #regex #viewprogramming

Apps I've Built

PhotoLocationChanger
Easily change the location on your photos and videos
Click here for more info about the app
PhotoDateChanger
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app