<?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: Regular Expressions in JavaScript, part 2</title>
	<atom:link href="http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 02 Sep 2010 20:23:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: TJ Loh</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-19606</link>
		<dc:creator>TJ Loh</dc:creator>
		<pubDate>Wed, 02 Sep 2009 16:16:53 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-19606</guid>
		<description>Great Post! Thanks</description>
		<content:encoded><![CDATA[<p>Great Post! Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8631</link>
		<dc:creator>James</dc:creator>
		<pubDate>Thu, 26 Mar 2009 08:26:40 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8631</guid>
		<description>Thanks for the comments!

@&lt;a href=&quot;#comment-8550&quot; rel=&quot;nofollow&quot;&gt;&lt;strong&gt;Valentino&lt;/strong&gt;&lt;/a&gt;, that looks like a very useful tool, thank you!

@&lt;a href=&quot;#comment-8624&quot; rel=&quot;nofollow&quot;&gt;&lt;strong&gt;Luke&lt;/strong&gt;&lt;/a&gt;, thank you for the suggestions! I had no idea &lt;code&gt;exec&lt;/code&gt; and &lt;code&gt;match&lt;/code&gt; behaved differently. I&#039;ll add that to the post. Also, just subscribed to http://blog.stevenlevithan.com/ - looks great!</description>
		<content:encoded><![CDATA[<p>Thanks for the comments!</p>
<p>@<a href="#comment-8550"><strong>Valentino</strong></a>, that looks like a very useful tool, thank you!</p>
<p>@<a href="#comment-8624"><strong>Luke</strong></a>, thank you for the suggestions! I had no idea <code>exec</code> and <code>match</code> behaved differently. I&#8217;ll add that to the post. Also, just subscribed to <a href="http://blog.stevenlevithan.com/">http://blog.stevenlevithan.com/</a> &#8211; looks great!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8624</link>
		<dc:creator>Luke</dc:creator>
		<pubDate>Thu, 26 Mar 2009 05:17:10 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8624</guid>
		<description>Great post.  Regex is definitely something that more js developers need to learn.  One correction and a couple suggestions for you, though...

Correction: str.match and regex.exec are only the same if the regex is not compiled with the g flag.  Against a g regex, match does not return capture groups, but every instance of the full match. exec returns the first match and any capture groups and updates the regex&#039;s lastIndex Property.  Future calls to exec will start from that index (useful in a while loop).

&lt;pre lang=&quot;javascript&quot;&gt;
var re = /f(\w)\1/g, str = &quot;Calling all foo, calling all faa&quot;;
str.match(re); // [&quot;foo&quot;,&quot;faa&quot;]
str.match(re); // [&quot;foo&quot;,&quot;faa&quot;]
re.exec(str);  // [&quot;foo&quot;,&quot;o&quot;] and re.lastIndex === 15
re.exec(str);  // [&quot;faa&quot;,&quot;a&quot;] and re.lastIndex === 32
&lt;/pre&gt;

Suggestions:
Unless you specifically need the data in your groups, use a non-capturing group, vis /(?:f&#124;ht)tps?/ won&#039;t internally store or return the f or ht string.  It makes for marginally faster code, but more importantly, you don&#039;t have to specify unused variables for dead captures if you have important capture groups afterward.

And your camelCaseCSS function doesn&#039;t need an inner regex. charAt(1) will do the work much faster.

&lt;pre lang=&quot;javascript&quot;&gt;
function camelCaseCSS(property) {
    return property.replace(/-\w/g, function(match){
        return match.charAt(1).toUpperCase();
    });
}
&lt;/pre&gt;

Another great resource for advanced regex know-how is Steven Levithan&#039;s blog Flagrant Badassery http://blog.stevenlevithan.com/</description>
		<content:encoded><![CDATA[<p>Great post.  Regex is definitely something that more js developers need to learn.  One correction and a couple suggestions for you, though&#8230;</p>
<p>Correction: str.match and regex.exec are only the same if the regex is not compiled with the g flag.  Against a g regex, match does not return capture groups, but every instance of the full match. exec returns the first match and any capture groups and updates the regex&#8217;s lastIndex Property.  Future calls to exec will start from that index (useful in a while loop).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> re <span class="sy0">=</span> <span class="co2">/f(\w)\1/g</span><span class="sy0">,</span> str <span class="sy0">=</span> <span class="st0">&quot;Calling all foo, calling all faa&quot;</span><span class="sy0">;</span>
str.<span class="me1">match</span><span class="br0">&#40;</span>re<span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// [&quot;foo&quot;,&quot;faa&quot;]</span>
str.<span class="me1">match</span><span class="br0">&#40;</span>re<span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// [&quot;foo&quot;,&quot;faa&quot;]</span>
re.<span class="me1">exec</span><span class="br0">&#40;</span>str<span class="br0">&#41;</span><span class="sy0">;</span>  <span class="co1">// [&quot;foo&quot;,&quot;o&quot;] and re.lastIndex === 15</span>
re.<span class="me1">exec</span><span class="br0">&#40;</span>str<span class="br0">&#41;</span><span class="sy0">;</span>  <span class="co1">// [&quot;faa&quot;,&quot;a&quot;] and re.lastIndex === 32</span></pre></div></div>

<p>Suggestions:<br />
Unless you specifically need the data in your groups, use a non-capturing group, vis /(?:f|ht)tps?/ won&#8217;t internally store or return the f or ht string.  It makes for marginally faster code, but more importantly, you don&#8217;t have to specify unused variables for dead captures if you have important capture groups afterward.</p>
<p>And your camelCaseCSS function doesn&#8217;t need an inner regex. charAt(1) will do the work much faster.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> camelCaseCSS<span class="br0">&#40;</span>property<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> property.<span class="me1">replace</span><span class="br0">&#40;</span><span class="co2">/-\w/g</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>match<span class="br0">&#41;</span><span class="br0">&#123;</span>
        <span class="kw1">return</span> match.<span class="me1">charAt</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Another great resource for advanced regex know-how is Steven Levithan&#8217;s blog Flagrant Badassery <a href="http://blog.stevenlevithan.com/">http://blog.stevenlevithan.com/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Valentino</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8550</link>
		<dc:creator>Valentino</dc:creator>
		<pubDate>Tue, 24 Mar 2009 20:33:22 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8550</guid>
		<description>I suggest this online tool for testing and creating RegEx: 

http://gskinner.com/RegExr/

When I started using it, creating regular expressions became actually fun!</description>
		<content:encoded><![CDATA[<p>I suggest this online tool for testing and creating RegEx: </p>
<p><a href="http://gskinner.com/RegExr/">http://gskinner.com/RegExr/</a></p>
<p>When I started using it, creating regular expressions became actually fun!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Graham B</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8532</link>
		<dc:creator>Graham B</dc:creator>
		<pubDate>Tue, 24 Mar 2009 14:24:34 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8532</guid>
		<description>Huh, I never knew replace() could accept a function. Nice one :)</description>
		<content:encoded><![CDATA[<p>Huh, I never knew replace() could accept a function. Nice one <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eugene</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8529</link>
		<dc:creator>eugene</dc:creator>
		<pubDate>Tue, 24 Mar 2009 13:26:28 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8529</guid>
		<description>great post
after reading your regex on testing for http, https, ftp...

i dug up an old regex i had for this, and seems completely bloated compared to your. great work.

here is my old one (stripped from a class)

&lt;pre lang=&quot;javascript&quot;&gt;
// site
Site:function()
{
	var pattern = /^(([f]&#124;[h])(t?tp)(s?)(:\/\/))?((www)(.))?/;
	var site	= window.content.location.host;
	site		= site.replace(pattern, &#039;&#039;);
	return site;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>great post<br />
after reading your regex on testing for http, https, <a href="http://ftp..">http://ftp..</a>.</p>
<p>i dug up an old regex i had for this, and seems completely bloated compared to your. great work.</p>
<p>here is my old one (stripped from a class)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// site</span>
Site<span class="sy0">:</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw2">var</span> pattern <span class="sy0">=</span> <span class="co2">/^(([f]|[h])(t?tp)(s?)(:\/\/))?((www)(.))?/</span><span class="sy0">;</span>
	<span class="kw2">var</span> site	<span class="sy0">=</span> window.<span class="me1">content</span>.<span class="me1">location</span>.<span class="me1">host</span><span class="sy0">;</span>
	site		<span class="sy0">=</span> site.<span class="me1">replace</span><span class="br0">&#40;</span>pattern<span class="sy0">,</span> <span class="st0">''</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw1">return</span> site<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Baa</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8487</link>
		<dc:creator>Baa</dc:creator>
		<pubDate>Mon, 23 Mar 2009 23:08:16 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8487</guid>
		<description>Great post James. Finally somebody encouraged me to really start learning regular expressions. Thanks! ;)</description>
		<content:encoded><![CDATA[<p>Great post James. Finally somebody encouraged me to really start learning regular expressions. Thanks! <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Nadel</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8482</link>
		<dc:creator>Ben Nadel</dc:creator>
		<pubDate>Mon, 23 Mar 2009 22:40:20 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8482</guid>
		<description>Awesome stuff. I am a huge fan of regular expressions in Javascript. I have not given the String.match() method a go yet. I will have to try that.</description>
		<content:encoded><![CDATA[<p>Awesome stuff. I am a huge fan of regular expressions in Javascript. I have not given the String.match() method a go yet. I will have to try that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Diogok</title>
		<link>http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/comment-page-1/#comment-8481</link>
		<dc:creator>Diogok</dc:creator>
		<pubDate>Mon, 23 Mar 2009 22:25:13 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=692#comment-8481</guid>
		<description>awesome!</description>
		<content:encoded><![CDATA[<p>awesome!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
