Converti les urls contenus dans une chaine en liens cliquables.
function autolink(str, attributes)
{
attributes = attributes || {};
var attrs = "";
for(name in attributes)
attrs += " "+ name +'="'+ attributes[name] +'"';
var reg = new RegExp("(\\s?)((http|https|ftp)://[^\\s<]+[^\\s<\.)])", "gim");
str = str.toString().replace(reg, '$1<a href="$2"'+ attrs +'>$2</a>');
return str;
}
string autolink ( string str [, object attributes ] )
Retourne une copie de la chaîne str dont les urls ont été encapsulées dans des balises <a>.
Exemple #1 Exemple avec autolink()
str = 'A link : http://example.com/?param=value#anchor.'; str = autolink(str); alert(str); // A link : <a href="http://example.com/?param=value#anchor">http://example.com/?param=value#anchor</a>.
Exemple #2 Exemple avec autolink()
str = 'http://example.com/';
str = autolink(str, {"target":"_blank"});
alert(str); // <a href="http://example.com/" target="_blank">http://example.com/</a>
Commentaire(s)
Poster un commentaire