<?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>A Dash Of Web and Mobile Development &#187; regex</title>
	<atom:link href="http://blog.iangclifton.com/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.iangclifton.com</link>
	<description>Discussing trends and technologies in web and mobile development</description>
	<lastBuildDate>Wed, 28 Jul 2010 17:06:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamically Escape Regular Expressions In JavaScript</title>
		<link>http://blog.iangclifton.com/2009/09/13/dynamically-escape-regular-expressions-in-javascript/</link>
		<comments>http://blog.iangclifton.com/2009/09/13/dynamically-escape-regular-expressions-in-javascript/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 03:35:07 +0000</pubDate>
		<dc:creator>Ian G. Clifton</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expression]]></category>

		<guid isPermaLink="false">http://blog.iangclifton.com/?p=37</guid>
		<description><![CDATA[I&#8217;m not sure why, but JavaScript doesn&#8217;t appear to have a good way of escaping regular expressions that are provided dynamically.  In my case, I was using a string provided as part of a JSON AJAX response, and I realized that it contained a question mark, which has special meaning in a regular expression. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure why, but JavaScript doesn&#8217;t appear to have a good way of escaping regular expressions that are provided dynamically.  In my case, I was using a string provided as part of a JSON AJAX response, and I realized that it contained a question mark, which has special meaning in a regular expression.  Here is my solution:<span id="more-37"></span></p>
<pre class="brush: jscript;">
String.prototype.escapeRegExp = function() {
    var specialChars = [ '$', '^', '*', '(', ')', '+', '[', ']', '{', '}', '\\', '|', '.', '?', '/' ];
    var regex = new RegExp('(\\' + specialChars.join('|\\') + ')', 'g');
    return this.replace(regex, '\\$1');
}
</pre>
<p>Basically, we modify the string object, adding an escapeRegExp method.  It seemed to make more sense to me to add this to the string object and not the RegExp object.  Now you can do something like <code>var pattern = new RegExp(dynamicRegex.escapeRegExp(), 'g');</code> (assuming dynamicRegex is a string object).  Then <var>pattern</var> can be passed as the first parameter in a string&#8217;s replace method.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.iangclifton.com/2009/09/13/dynamically-escape-regular-expressions-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
