Converting links to links
I have an jQuery ajax function which returns data
. This data
contains text such as:
Lorem ipsum dolor sit amet, http://www.consectetur.com adipiscing elit. Nam quis purus id nulla http://www.bibendum.com iaculis. Phasellus varius pellentesque libero, ac congue n开发者_开发知识库eque dignissim eu
How do I convert this too:
Lorem ipsum dolor sit amet, <a href="http://www.consectetur.com">http://www.consectetur.com</a> adipiscing elit. Nam quis purus id nulla a href="http://www.bibendum.com">http://www.bibendum.com</a> iaculis. Phasellus varius pellentesque libero, ac congue neque dignissim eu
Stand back! I know regular expressions:
var anchored = myInputData.replace( /(http:\/\/[^\s]{5,})/g,
"<a href=\"$1\">$1</a>")
(Globally replaces everything which starts with http:// and continues until there is whitespace with the pattern you describe)
I would use regex matches to find "http://...... " and add the anchor stuff around it.
Excellent reference for javascript regex is here
精彩评论