<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Parsing Twitter Usernames, Hashtags and URLs with JavaScript</title>
	<atom:link href="http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Tue, 24 Jan 2012 10:54:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Eric Atallah</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-685</link>
		<dc:creator>Eric Atallah</dc:creator>
		<pubDate>Tue, 06 Dec 2011 17:16:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-685</guid>
		<description>Hello,

Is there anyway to add:  target=&quot;_blank&quot;  to the hyperlink so it opens in a new window/tab?

Thanks!</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>Is there anyway to add:  target=&#8221;_blank&#8221;  to the hyperlink so it opens in a new window/tab?</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-683</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Wed, 30 Nov 2011 20:13:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-683</guid>
		<description>Hi,

Great post, this solved my exact problem.  However, I still can&#039;t figure out how to make the links open up in a new window/tab.  How can we modify the parseURL() function to set the target attribute of the anchor tag to _blank?

Thanks,

-Eric</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Great post, this solved my exact problem.  However, I still can&#8217;t figure out how to make the links open up in a new window/tab.  How can we modify the parseURL() function to set the target attribute of the anchor tag to _blank?</p>
<p>Thanks,</p>
<p>-Eric</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aran Cloverink Chananar</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-682</link>
		<dc:creator>Aran Cloverink Chananar</dc:creator>
		<pubDate>Thu, 24 Nov 2011 08:10:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-682</guid>
		<description>thx!</description>
		<content:encoded><![CDATA[<p>thx!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: andré</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-678</link>
		<dc:creator>andré</dc:creator>
		<pubDate>Sat, 29 Oct 2011 04:31:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-678</guid>
		<description>Life saver, great code =] thanks</description>
		<content:encoded><![CDATA[<p>Life saver, great code =] thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A83</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-677</link>
		<dc:creator>A83</dc:creator>
		<pubDate>Tue, 18 Oct 2011 14:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-677</guid>
		<description>If I would like to add an class to each of these links so I could style hashtags, urls and usernames differently how would I do that?

Thanks!</description>
		<content:encoded><![CDATA[<p>If I would like to add an class to each of these links so I could style hashtags, urls and usernames differently how would I do that?</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marcus Bointon</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-676</link>
		<dc:creator>Marcus Bointon</dc:creator>
		<pubDate>Wed, 28 Sep 2011 10:48:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-676</guid>
		<description>All of the patterns used in here will match things that look like twitter elements but are not, for example # that&#039;s part of a URL, @ that&#039;s part of an email address. They also match misformatted items like @@  and ##. Eduardo Cancino suggested a fix that won&#039;t really work as it will only match at the start of a string (which will catch most replies); you need to enforce that they are not preceeded by a word boundary. Twitter user names can&#039;t contain &#039;-&#039;, so that should not be included in the class (which is wrong anyway as &#039;-&#039; has a special meaning unless it&#039;s the last character in a range). Conveniently, the chars allowed in a twitter id are exactly those included in the w class. Twitter IDs are limited to 20 characters, so anything that&#039;s over 20 chars is not a twitter ID, so the pattern should assert that too.

Hashtags are not limited to ASCII, so a better pattern for that should allow anything but whitespace (and by experimentation with twitter&#039;s own client, some punctuation, extend as you like).

So, revised regexes (should work in both PHP and JS) are:

Twitter ID:

/(?!b)@[w]{1,20}(?![w])/

Hashtag:

/(?!b)#[^s.&#039;&quot;]+/

So given this tweet:

#hello @xyz @hello #hashtag nobody@example.com #blah @iamnotatwitteridatall http://www.examplecom/index.php#fragment #&lt;3 #bababa

It should extract @hello and @xyz twitter IDs (but NOT @example or @iamnotatwitteridatall) and #hello, #hashtag, #&lt;3 and #bababa (but NOT #fragment).</description>
		<content:encoded><![CDATA[<p>All of the patterns used in here will match things that look like twitter elements but are not, for example # that&#8217;s part of a URL, @ that&#8217;s part of an email address. They also match misformatted items like @@  and ##. Eduardo Cancino suggested a fix that won&#8217;t really work as it will only match at the start of a string (which will catch most replies); you need to enforce that they are not preceeded by a word boundary. Twitter user names can&#8217;t contain &#8216;-&#8217;, so that should not be included in the class (which is wrong anyway as &#8216;-&#8217; has a special meaning unless it&#8217;s the last character in a range). Conveniently, the chars allowed in a twitter id are exactly those included in the w class. Twitter IDs are limited to 20 characters, so anything that&#8217;s over 20 chars is not a twitter ID, so the pattern should assert that too.</p>
<p>Hashtags are not limited to ASCII, so a better pattern for that should allow anything but whitespace (and by experimentation with twitter&#8217;s own client, some punctuation, extend as you like).</p>
<p>So, revised regexes (should work in both PHP and JS) are:</p>
<p>Twitter ID:</p>
<p>/(?!b)@[w]{1,20}(?![w])/</p>
<p>Hashtag:</p>
<p>/(?!b)#[^s.'"]+/</p>
<p>So given this tweet:</p>
<p>#hello @xyz @hello #hashtag <a href="mailto:nobody@example.com">nobody@example.com</a> #blah @iamnotatwitteridatall <a href="http://www.examplecom/index.php#fragment" rel="nofollow">http://www.examplecom/index.php#fragment</a> #&lt;3 #bababa</p>
<p>It should extract @hello and @xyz twitter IDs (but NOT @example or @iamnotatwitteridatall) and #hello, #hashtag, #&lt;3 and #bababa (but NOT #fragment).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eduardo Cancino</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-657</link>
		<dc:creator>Eduardo Cancino</dc:creator>
		<pubDate>Fri, 15 Jul 2011 16:10:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-657</guid>
		<description>If you have an email address parser the username parser getts it wrong, to correct this:

Just added ^ at the beginning of pattern, hope it helps







p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #413af9}
span.s1 {color: #901a66}
span.s2 {color: #000000}
span.s3 {color: #413af9}
span.Apple-tab-span {white-space:pre}



String.prototype.parseUsername = function() {
	return this.replace(/^[@]+[A-Za-z0-9-_]+/g, function(u) {
		var username = u.replace(&quot;@&quot;,&quot;&quot;)
		return u.link(&quot;http://twitter.com/&quot;+username);
	});
};</description>
		<content:encoded><![CDATA[<p>If you have an email address parser the username parser getts it wrong, to correct this:</p>
<p>Just added ^ at the beginning of pattern, hope it helps</p>
<p>p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}<br />
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #413af9}<br />
span.s1 {color: #901a66}<br />
span.s2 {color: #000000}<br />
span.s3 {color: #413af9}<br />
span.Apple-tab-span {white-space:pre}</p>
<p>String.prototype.parseUsername = function() {<br />
	return this.replace(/^[@]+[A-Za-z0-9-_]+/g, function(u) {<br />
		var username = u.replace(&#8220;@&#8221;,&#8221;")<br />
		return u.link(&#8220;http://twitter.com/&#8221;+username);<br />
	});<br />
};</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eduardo Cancino</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-656</link>
		<dc:creator>Eduardo Cancino</dc:creator>
		<pubDate>Fri, 15 Jul 2011 15:33:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-656</guid>
		<description>If you have an email address it recognizes it as twitter username, even if it is on a link</description>
		<content:encoded><![CDATA[<p>If you have an email address it recognizes it as twitter username, even if it is on a link</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eduardo Hertz</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-636</link>
		<dc:creator>Eduardo Hertz</dc:creator>
		<pubDate>Thu, 05 May 2011 20:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-636</guid>
		<description>The regex to parse hashtags is getting only the first hashtag. If a tweet has two hashtags, for example, it will parse only the first one.

To correct this, just put a &quot;g&quot; in the end of the regex. Like this:


String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
		var tag = t.replace(&quot;#&quot;,&quot;%23&quot;)
		return t.link(&quot;http://search.twitter.com/search?q=&quot;+tag);
	});
};


Thank you for the post!</description>
		<content:encoded><![CDATA[<p>The regex to parse hashtags is getting only the first hashtag. If a tweet has two hashtags, for example, it will parse only the first one.</p>
<p>To correct this, just put a &#8220;g&#8221; in the end of the regex. Like this:</p>
<p>String.prototype.parseHashtag = function() {<br />
	return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {<br />
		var tag = t.replace(&#8220;#&#8221;,&#8221;%23&#8243;)<br />
		return t.link(&#8220;http://search.twitter.com/search?q=&#8221;+tag);<br />
	});<br />
};</p>
<p>Thank you for the post!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Conor</title>
		<link>http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-javascript/comment-page-1#comment-616</link>
		<dc:creator>Conor</dc:creator>
		<pubDate>Mon, 07 Mar 2011 13:36:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=646#comment-616</guid>
		<description>Thanks very much for this code. I&#039;ve used it in a Google Maps &amp; Twitter Mashup. Very handy. Cheers</description>
		<content:encoded><![CDATA[<p>Thanks very much for this code. I&#8217;ve used it in a Google Maps &amp; Twitter Mashup. Very handy. Cheers</p>
]]></content:encoded>
	</item>
</channel>
</rss>

