# Replace string block containing a URL with html hyperlink,

**URL:** <https://forum.kirupa.com/t/replace-string-block-containing-a-url-with-html-hyperlink/311037>\
**Category:** Uncategorized\
**Created:** [June 25, 2010, 7:55pm UTC](https://forum.kirupa.com/t/replace-string-block-containing-a-url-with-html-hyperlink/311037 "2010-06-25T19:55:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomthinks](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tomthinks](https://forum.kirupa.com/u/tomthinks)\
**Post date:** [June 25, 2010, 7:55pm UTC](https://forum.kirupa.com/t/replace-string-block-containing-a-url-with-html-hyperlink/311037/1 "2010-06-25T19:55:27Z")

</div>

Hi there. The basic idea is that I have a string that contains a user’s comment, which could potentially contain URLs, so I wanted to take a string like:

“Gee, the folks in the [http://www.kirupa.com](http://www.kirupa.com) forum are so nice.”

And using pattern replace, replace the URL with an HTML hyperlink, so the string would end up being:

“Gee, the folks in the \<a href=‘[http://www.kirupa.com](http://www.kirupa.com)’\>[http://www.kirupa.com](http://www.kirupa.com)\</a\> forum are so nice.”

I have found code that works perfectly, but only if the string is ONLY a url:

```auto

var protocol:String = "((?:http|ftp)://)"; 
var urlPart:String = "([a-z0-9_-]+\.[a-z0-9_-]+)"; 
var optionalUrlPart:String = "(\.[a-z0-9_-]*)"; 
var urlPattern:RegExp = new RegExp(protocol + urlPart + optionalUrlPart, "gi"); 
newString = originalString.replace(urlPattern, "<a href='$1$2$3'>$1$2$3</a>");

```

So the above code would work perfectly if the original string were:

“[http://www.kirupa.com](http://www.kirupa.com)”

So my question is how can I alter the pattern replace code so that it accommodates a string containing random text, as well as a URL.

Any thoughts would be very much appreciated. Thank you.
