I have text of a form where there are paragraphs of text with urls interspersed. I would like to parse the string creating html links from the urls and using the following text as the descriptive link text i.e.
possibly some text here http://www.somewebsite.com/some/path/somepage.html descriptive text which may or may not be present
into
<a href="http://www.somewebsite.com/some/path/somepage.html">descriptive text which may or may not be present</a>
This SO article, JS: Find URLs in Text, Make Links, is relevant to what I'm attempting to do but simply places the url as the text within the anchor element.
I am successfully matching the url with
var urlRE= new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+");
but am unsure how to perform the match afterwards.
I came across this post Regex - Matching text AFTER certain characters which seems applicable. I've attempted to wrap my RE in /(?<=my url pattern here).+/ but get an error stating that there is an invalid group and that this results in an invalid RE.
In that post J-Law mentions that
Variable-length lookbehinds aren’t allowed
Is this what I'm attempting to do?
Since I'm already matching the url I feel like I could easily do some substring math to get the desired results.
I'm just using this as an attempt to learn more about regex.
Thanks
([^ ])+. If there is such a thing assite.XX, it won't match. You could change it to([^ ])*and I don't think it would matter much.