<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alex Little &#187; url replace</title>
	<atom:link href="http://alexlittle.net/blog/tag/url-replace/feed/" rel="self" type="application/rss+xml" />
	<link>http://alexlittle.net/blog</link>
	<description></description>
	<lastBuildDate>Mon, 21 May 2012 17:31:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Regex for replacing URL with anchor tag</title>
		<link>http://alexlittle.net/blog/2009/06/26/regex-for-replacing-url-with-anchor-tag/</link>
		<comments>http://alexlittle.net/blog/2009/06/26/regex-for-replacing-url-with-anchor-tag/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 09:38:12 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[javscript]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[url replace]]></category>

		<guid isPermaLink="false">http://alexlittle.net/blog/?p=1384</guid>
		<description><![CDATA[Despite there being 1001 regexes on the web for matching a URL in a string and then replacing it with an anchor tag (commonly used in chat applications etc.), I couldn&#8217;t find one which did exactly what I wanted, so I thought I&#8217;d share my regex for replacing URLs with anchor tags. Firstly, the problem [...]]]></description>
			<content:encoded><![CDATA[<p>Despite there being 1001 regexes on the web for matching a URL in a string and then replacing it with an anchor tag (commonly used in chat applications etc.), I couldn&#8217;t find one which did exactly what I wanted, so I thought I&#8217;d share my regex for replacing URLs with anchor tags.</p>
<p>Firstly, the problem with all the other regexs I found.  Take for example the following text block:</p>
<p><code>you should have a look at &lt;a href='http://example1.com'&gt;http://example1.com&lt;/a&gt;, and http://example2.com</code></p>
<p>All of the regexes I found for matching and replacing URLs with anchor tags would have transformed my text block into:</p>
<p><code>you should have a look at &lt;a href='&lt;a href='http://example1.com'&gt;http://example1.com&lt;/a&gt;'&gt;http://example1.com&lt;/a&gt;, and &lt;a href='http://example2.com'&gt;http://example2.com&lt;/a&gt;</code></p>
<p>None of the example regexes I found coped with the possibility that some URLs may already be contained in an anchor tag, as in the example above, or in other instances where you don&#8217;t want to insert an anchor tag, such as: <code>&lt;img src='http://example.com/logo.png'/&gt;</code></p>
<p>The regex and code I am now using to get around this problem is (in Javascript):<br />
<code><br />
var mystring = "text with some URLs in";<br />
var myregex = /(^|[^'">])((ftp|http|https):\/\/(\S+))(\b|$)/gi;<br />
var newstring = mystring.replace(myregex ,"$1&lt;a href='$2'&gt;$2&lt;/a&gt;");<br />
</code></p>
<p>All appears to be working well so far, but I&#8217;m sure my regex is far from perfect. This regex doesn&#8217;t check that the URL is really well formed, for example valid top level domain etc.</p>
<p>So please post any possible improvements below, in the meantime I hope it&#8217;s useful <img src='http://alexlittle.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://alexlittle.net/blog/2009/06/26/regex-for-replacing-url-with-anchor-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

