1

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 neque 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
oshirowanen
  • 15,297
  • 82
  • 198
  • 350

2 Answers2

3

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)

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

I would use regex matches to find "http://...... " and add the anchor stuff around it.

Excellent reference for javascript regex is here

kasdega
  • 18,396
  • 12
  • 45
  • 89