Skip to content

Instantly share code, notes, and snippets.

@theeluwin
Created July 28, 2014 08:39
Show Gist options
  • Select an option

  • Save theeluwin/e69545a3badd011f5179 to your computer and use it in GitHub Desktop.

Select an option

Save theeluwin/e69545a3badd011f5179 to your computer and use it in GitHub Desktop.
useful angular filters
// converts link in text into anchor
app.filter("link_finder", function(){
return function(input){
return input? input.replace(/(\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+)/g, "<a href='$1' target='new'>$1</a>") : "";
};
});
// escape '<' and '>'
app.filter("escape_html", function(){
return function(input){
return input? input.replace(/</g, "&lt;").replace(/>/g, "&gt;") : "";
};
});
// allow <br> only
app.filter("enable_br", function(){
return function(input){
return input? input.replace(/&lt;br\/?\ ?&gt;/g, "<br>").replace(/\n/g, "<br>") : "";
};
});
// forcely add 'http(s)?://' infront of link, if not
app.filter("external_link", function(){
return function(input){
if(typeof(input) == "string") {
return input.substr(0, 4) == "http"? input : "http://" + input;
} else {
return input;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment