<?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>Simon Whatley &#187; tag</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/tag/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Parsing Twitter Usernames, Hashtags and URLs with ColdFusion</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-coldfusion</link>
		<comments>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-coldfusion#comments</comments>
		<pubDate>Fri, 01 May 2009 11:24:07 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[GPS logger]]></category>
		<category><![CDATA[Holux M-241 GPS Receiver]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[New Brunswick]]></category>
		<category><![CDATA[online resource]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[username]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=1907</guid>
		<description><![CDATA[Some time ago, well almost a year ago actually, I posted an article called Parsing Twitter Usernames, Hashtags and URLs with JavaScript. From that article, it became immediately apparent that this was an issue many people were confronting and one that required an answer. Now, belatedly, it is the turn of ColdFusion to get the Twitter love.]]></description>
			<content:encoded><![CDATA[<p>Some time ago, well almost a year ago actually, I posted an article called <a href="/parsing-twitter-usernames-hashtags-and-urls-with-javascript">Parsing Twitter Usernames, Hashtags and URLs with JavaScript</a>. From that article, it became immediately apparent that this was an issue many people were confronting and one that required an answer. Now, belatedly, it is the turn of ColdFusion to get the Twitter love.</p>
<p>Compared to JavaScript it is far easier to parse the <abbr title="Univeral Resource Locator">URL</abbr>s, Usernames and Hashtags in a tweet using ColdFusion and minor amendments to the regular expressions used in the JavaScript code.</p>
<p>Below is an example tweet that I&#8217;ll use for this post.</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #800000;">&lt;cfset</span> myTweet <span style="color: #0000ff">=</span> <span style="color: #009900;">&quot;Woot! I've just taken receipt of my Holux M-241 GPS logger. Good call @fordie. http://bit.ly/2RsAu ##holux ##gpslogger&quot;</span> <span style="color: #0000ff;">/</span><span style="color: #800000;">&gt;</span></span></pre></div></div>

<p><abbr title="Nota bene (please note)">NB</abbr>. For the purpose of this test, I need to double-hash the hashtags to prevent ColdFusion throwing an error.</p>
<h3>Parsing URLs as Links to the resource</h3>
<p>We can simply demonstrate the parsing of the link with the following code in the body of the page:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #800000;">&lt;cfset</span> myTweet <span style="color: #0000ff">=</span> <span style="color: #800080;">REReplace</span><span style="color: #000000;">&#40;</span>myTweet,<span style="color: #009900;">'([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&amp;amp;\?\/.=]+)'</span>,<span style="color: #009900;">'&lt;a href=&quot;\1&quot;&gt;</span></span>\1<span style="color: #333333;"><span style="color: #800000;">&lt;</span><span style="color: #0000ff;">/</span>a<span style="color: #0000ff;">&gt;</span></span>','ALL') /&gt;</pre></div></div>

<p><abbr title="Nota bene (please note)">NB</abbr>. The <code>\1</code> is a back reference to part of the regular expression match. A backreference stores the part of the string matched by the part of the regular expression inside the parentheses. This means you can reuse it inside the regular expression, or afterwards as I am doing in each of these examples.</p>
<p>The resultant HTML generated is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Woot! I've just taken receipt of my Holux M-241 GPS logger. Good call @fordie. &lt;a href=&quot;http://bit.ly/2RsAu&quot;&gt;http://bit.ly/2RsAu&lt;/a&gt; #holux #gpslogger</pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Parsing Usernames as Links to Twitter</h3>
<p>Following on from the <abbr title="Universal Resource Locator">URL</abbr> example above, we can apply a similar methodology to Twitter usernames since they can also be <abbr title="Universal Resource Locator">URL</abbr>s to their associated Twitter page.</p>
<p>We can simply demonstrate this with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #800000;">&lt;cfset</span> myTweet <span style="color: #0000ff">=</span> <span style="color: #800080;">REReplace</span><span style="color: #000000;">&#40;</span>myTweet,<span style="color: #009900;">'[@]+([A-Za-z0-9-_]+)'</span>,<span style="color: #009900;">'&lt;a href=&quot;http://twitter.com/\1&quot; rel=&quot;nofollow&quot;&gt;</span></span>@\1<span style="color: #333333;"><span style="color: #800000;">&lt;</span><span style="color: #0000ff;">/</span>a<span style="color: #0000ff;">&gt;</span></span>','ALL') /&gt;</pre></div></div>

<p>The regular expression in this case finds all instances of <code>@username</code>. The Twitter <abbr title="Universal Resource Locator">URL</abbr> is then applied to the username.</p>
<p>The resultant HTML generated is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Woot! I've just taken receipt of my Holux M-241 GPS logger. Good call &lt;a href=&quot;http://twitter.com/fordie&quot; rel=&quot;nofollow&quot;&gt;@fordie&lt;/a&gt;. http://bit.ly/2RsAu #holux #gpslogger</pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Parsing Hashtags as Links to Twitter’s Search</h3>
<p>Finally, Twitter also allows user’s to create Hastags within their posts. Hashtags are a community-driven convention for adding additional context and metadata to your tweets. Like regular <abbr title="Universal Resource Locator">URL</abbr>s and usernames, Hastags can been parsed as a <abbr title="Universal Resource Locator">URL</abbr> to an online resource, in this case, Twitter’s search.</p>
<p>We can simply demonstrate this with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #800000;">&lt;cfset</span> myTweet <span style="color: #0000ff">=</span> <span style="color: #800080;">REReplace</span><span style="color: #000000;">&#40;</span>myTweet,<span style="color: #009900;">'[##]+([A-Za-z0-9-_]+)'</span>,<span style="color: #009900;">'&lt;a href=&quot;http://search.twitter.com/search?q=%23\1&quot; rel=&quot;nofollow&quot;&gt;</span></span><span style="color: #0000ff;">##</span>\1<span style="color: #333333;"><span style="color: #800000;">&lt;</span><span style="color: #0000ff;">/</span>a<span style="color: #0000ff;">&gt;</span></span>','ALL') /&gt;</pre></div></div>

<p>The regular expression in this case finds all instances of <code>#hashtag</code>. The Twitter Search <abbr title="Universal Resource Locator">URL</abbr> is then applied to the hashtag.</p>
<p>The resultant HTML generated is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Woot! I've just taken receipt of my Holux M-241 GPS logger. Good call @fordie. http://bit.ly/2RsAu &lt;a href=&quot;http://search.twitter.com/search?q=%23holux&quot; rel=&quot;nofollow&quot;&gt;#holux&lt;/a&gt; &lt;a href=&quot;http://search.twitter.com/search?q=%23ipslogger&quot; rel=&quot;nofollow&quot;&gt;#gpslogger&lt;/a&gt;</pre></div></div>

<h3>All in one</h3>
<p>So, putting all the regular expressions together, you would end up with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Woot! I've just taken receipt of my Holux M-241 GPS logger. Good call &lt;a href=&quot;http://twitter.com/fordie&quot; rel=&quot;nofollow&quot;&gt;@fordie&lt;/a&gt;. &lt;a href=&quot;http://bit.ly/2RsAu&quot;&gt;http://bit.ly/2RsAu&lt;/a&gt; &lt;a href=&quot;http://search.twitter.com/search?q=%23holux&quot; rel=&quot;nofollow&quot;&gt;#holux&lt;/a&gt; &lt;a href=&quot;http://search.twitter.com/search?q=%23gpslogger&quot; rel=&quot;nofollow&quot;&gt;#ipslogger&lt;/a&gt;</pre></div></div>

<p>Which translates as the more useful tweet:</p>
<p>Woot! I&#8217;ve just taken receipt of my Holux M-241 GPS logger. Good call <a href="http://twitter.com/fordie" rel="nofollow">@fordie</a>. <a href="http://bit.ly/2RsAu">http://bit.ly/2RsAu</a> <a href="http://search.twitter.com/search?q=%23holux" rel="nofollow">#holux</a> <a href="http://search.twitter.com/search?q=%23gpslogger" rel="nofollow">#gpslogger</a></p>
<h3>Where to take it next</h3>
<p>Wrap these code snippets up into <a href="/examples/twitter/twitterise/twitterise.txt">a simple twitterise function</a> could be a good starter for ten. Following that, we could also create a simple Twitter feed reader, but I&#8217;ll leave that up to you to develop.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-coldfusion/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Parsing Twitter Usernames, Hashtags and URLs with JavaScript</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript</link>
		<comments>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript#comments</comments>
		<pubDate>Mon, 21 Jul 2008 15:10:48 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[New Brunswick]]></category>
		<category><![CDATA[online musings]]></category>
		<category><![CDATA[online resource]]></category>
		<category><![CDATA[parseURL]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[Rob Douglas]]></category>
		<category><![CDATA[Simon Whatley]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[username]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646</guid>
		<description><![CDATA[As part of an AIR project that I have been working on with my good friend Rob, we came across the need to parse a number of URLs within the text of a Twitter post. This may not sound too easy at first, but thanks to the prototype property available on JavaScript objects, our task was a relatively simple one.]]></description>
			<content:encoded><![CDATA[<p><strong>Updated 10/05/2011</strong></p>
<p>As part of an <acronym title="Adobe Integrated Runtime">AIR</acronym> project that I have been working on with my good friend <a href="http://www.anucreative.com" title="Rob Douglas" target="_blank" rel="nofollow">Rob</a>, we came across the need to parse a number of <abbr title="Universal Resource Locator">URL</abbr>s within the text of a <a href="http://twitter.com" title="Twitter" target="_blank" rel="nofollow">Twitter</a> post. This may not sound too easy at first, but thanks to the prototype property available on JavaScript objects, our task was a relatively simple one.</p>
<p>The prototype object of JavaScript is a pre-built object that simplifies the process of adding custom properties or methods to all instances of an object. For example, there is not a <code>trim()</code> method available on the <code>String</code> class, therefore, through the wizardry of regular expressions and the prototype property, I can add one.</p>
<p>You simply need to specify <code>String.prototype</code> before your method definition. e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">trim</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\s+|\s+$/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>With this in mind, we can add methods to our <code>String</code> class, at runtime, that will allow us to manipulate the text string that is passed back in a Twitter <acronym title="JavaScript Object Notation">JSON</acronym> packet.</p>
<h3>The Goal</h3>
<p>To auto-magically parse different types of links within a text string. We will look at standard <abbr title="Universal Resource Locator">URL</abbr> links, links applied to <a href="http://twitter.com" title="Twitter" target="_blank" rel="nofollow">Twitter</a> usernames and those applied to <a href="http://twitter.pbwiki.com/Hashtags" title="Twitter Hashtags" target="_blank" rel="nofollow">Hashtags</a>.</p>
<h3>Demo</h3>
<p>The demonstration simply takes a test string and outputs it to the screen using JavaScript.</p>
<p><a href="http://www.simonwhatley.co.uk/examples/twitter/prototype/">See the demo in action.</a></p>
<h3>Parsing URLs as Links to the resource</h3>
<p>First we create a custom method of the <code>String.prototype</code> property called <code>parseURL</code>. When invoked on a string, the regular expression finds any instance of a <abbr title="Universal Resource Locator">URL</abbr> and will wrap the <abbr title="Universal Resource Locator">URL</abbr> with an <abbr title="HyperText Markup Language">HTML</abbr> anchor, with the correct <code>href</code> attribute and value applied.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">parseURL</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&amp;~\?\/.=]+/g</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> url.<span style="color: #660066;">link</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Demo 1.</strong></p>
<p>We can simply demonstrate the parsing of the link with the following code in the body of the page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
var test = &quot;Simon Whatley's online musings can be found at: http://www.simonwhatley.co.uk&quot;;
document.write(test.parseURL());
&lt;/script&gt;</pre></div></div>

<p>In the above example, a simple string variable is created called <code>test</code>, which contains a <abbr title="Universal Resource Locator">URL</abbr>. The text does not contain any <abbr title="HyperText Markup Language">HTML</abbr> at this stage. We then write out the <code>test</code> variable applying the <code>parseURL()</code> method to it.</p>
<p>The resultant <abbr title="HyperText Markup Language">HTML</abbr> generated is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Simon Whatley's online musings can be found at: &lt;a href=&quot;http://www.simonwhatley.co.uk&quot;&gt;http://www.simonwhatley.co.uk&lt;/a&gt;</pre></div></div>

<p>When rendered in a browser, the code becomes a hyper-link.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Parsing Usernames as Links to Twitter</h3>
<p>Following on from the <abbr title="Universal Resource Locator">URL</abbr> example above, we can apply a similar methodology to Twitter usernames since they can also be <abbr title="Universal Resource Locator">URL</abbr>s to their associated Twitter page.</p>
<p>Again we create a custom method of the <code>String.prototype</code> property, this time we&#8217;ll called it <code>parseUser</code>. The regular expression in this case finds all instances of <code>@username</code>. We then simply replace the <code>@</code> as this is not part of the actual username. The Twitter <abbr title="Universal Resource Locator">URL</abbr> is then applied to the username.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">parseUsername</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[@]+[A-Za-z0-9-_]+/g</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>u<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> username <span style="color: #339933;">=</span> u.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;@&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> u.<span style="color: #660066;">link</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://twitter.com/&quot;</span><span style="color: #339933;">+</span>username<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Demo 2.</strong></p>
<p>We can simply demonstrate this with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
var test = &quot;@whatterz is writing a post about JavaScript.&quot;;
document.writeln(test.parseUsername());
&lt;/script&gt;</pre></div></div>

<p>The resultant <abbr title="HyperText Markup Language">HTML</abbr> generated is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;http://twitter.com/whatterz&quot;&gt;@whatterz&lt;/a&gt; is writing a post about JavaScript</pre></div></div>

<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Parsing Hashtags as Links to Twitter&#8217;s Search</h3>
<p>Finally, Twitter also allows user&#8217;s to create Hastags within their posts. Hashtags are a community-driven convention for adding additional context and metadata to your tweets. Like regular <abbr title="Universal Resource Locator">URL</abbr>s and usernames, Hastags can been parsed as a <abbr title="Universal Resource Locator">URL</abbr> to an online resource, in this case, Twitter&#8217;s search.</p>
<p>Again we create a custom method of the <code>String.prototype</code> property, this time we&#8217;ll called it <code>parseHashtag</code>. The regular expression in this case finds all instances of <code>#hashtag</code>. The Twitter Search <abbr title="Universal Resource Locator">URL</abbr> is then applied to the hashtag.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">parseHashtag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[#]+[A-Za-z0-9-_]+/g</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> tag <span style="color: #339933;">=</span> t.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;%23&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> t.<span style="color: #660066;">link</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://search.twitter.com/search?q=&quot;</span><span style="color: #339933;">+</span>tag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Demo 3.</strong></p>
<p>We can simply demonstrate this with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
var test = &quot;Simon is writing a post about #twitter and parsing hashtags as URLs&quot;;
document.writeln(test.parseHashtag());
&lt;/script&gt;</pre></div></div>

<p>The resultant <abbr title="HyperText Markup Language">HTML</abbr> generated is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Simon is writing a post about &lt;a href=&quot;http://search.twitter.com/search?q=%23twitter&quot;&gt;#twitter&lt;/a&gt; and parsing hashtags as URLs</pre></div></div>

<p>NB. Twitter&#8217;s search was originally provided by Summize. However, as of July 2008, they have been bought by Twitter and the search can be found at <a href="http://search.twitter.com" title="Twitter Search" target="_blank" rel="nofollow">http://search.twitter.com</a>.</p>
<h3>Where to take it next</h3>
<p>Using the above code, we can now create a simple Twitter feed reader. Using, for example jQuery, to get and parse the Twitter <acronym title="JavaScript Object Notation">JSON</acronym> packet we can then apply the prototype methods to the text entries.</p>
<p>It is also worth noting that it is possible to cascade the methods, so we can do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
var test = &quot;@whatterz is writing a blog post about #twitter, which can be found at http://www.simonwhatley.co.uk&quot;;
document.writeln(test.parseURL().parseUsername().parseHashtag());
&lt;/script&gt;</pre></div></div>

<h3>Download the code</h3>
<p>The <a href="http://www.simonwhatley.co.uk/examples/twitter/prototype/">example code</a> can be downloaded from the demo page.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/feed</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
	</channel>
</rss>

