<?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>Barryvan &#187; string</title>
	<atom:link href="http://www.barryvan.com.au/tag/string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barryvan.com.au</link>
	<description>Music, Programming, Design</description>
	<lastBuildDate>Wed, 23 Jun 2010 04:18:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Limiting the contents of a string via RegEx</title>
		<link>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/</link>
		<comments>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 08:10:49 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=332</guid>
		<description><![CDATA[Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex [...]]]></description>
			<content:encoded><![CDATA[<p>Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex you supply.</p>
<pre class="brush: javascript">String.implement({
	limitContent: function(allowedRegex) {
		return $splat(this.match(allowedRegex)).join(&#039;&#039;);
	}
});</pre>
<p>Basically, the function takes the result of evaluating the regular expression on the string, converts it into an array if it isn&#8217;t one, and then joins the array&#8217;s elements together with an empty string.</p>
<h2>Examples:</h2>
<pre class="brush: javascript">console.log(&quot;12345&quot;.limitContent(/.{4}/)); // Only allow four characters
console.log(&quot;joe@mail.com&quot;.limitContent(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/)); // Only allow email addresses
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
