I'm trying to create an autolink feature in javascript that automatically turns urls into links while the user types (ContentEditable div).
I use this regex:
var text = 'Some text containing URLs';
var exp = /(\b(http):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var newtext = text.replace(exp,"<a href='$1'>$1</a>");
The code above works fine, but because the code gets called each time a user types, recursion occurs:
<a href='<a href='<a href=' etc.
How can I avoid this from happening while still having the script update the text as a user types?
So the question is (thanks @putvande): how do I check if a URL doesn't already contain:
<a href='...
(I'm not really handy with regex)