Last active
December 31, 2015 13:19
-
-
Save jackabox/ae1ade3314bf2e498d59 to your computer and use it in GitHub Desktop.
Autolinking strings in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment