<?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>Guillermo Rauch&#039;s Devthought &#187; highlighting</title>
	<atom:link href="http://www.devthought.com/tag/highlighting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devthought.com</link>
	<description></description>
	<lastBuildDate>Mon, 30 Jan 2012 16:38:59 +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>JavaScript RegExp based highlighting for MooTools and jQuery</title>
		<link>http://www.devthought.com/2009/04/04/javascript-regexp-based-highlighting-function-for-mootools-and-jquery/</link>
		<comments>http://www.devthought.com/2009/04/04/javascript-regexp-based-highlighting-function-for-mootools-and-jquery/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 18:55:07 +0000</pubDate>
		<dc:creator>Guillermo Rauch</dc:creator>
				<category><![CDATA[Client side]]></category>
		<category><![CDATA[highlighting]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://devthought.com/?p=725</guid>
		<description><![CDATA[How it works A regular expression looks for text outside HTML tags. It uses a callback function to perform replacements to simulate native lookahead support. When is this sort of replacement suitable ? Remote HTML responses (ajax) highlighting Autocompleters suggestion &#8230; <a href="http://www.devthought.com/2009/04/04/javascript-regexp-based-highlighting-function-for-mootools-and-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>How it works</h3>
<p>A regular expression looks for text outside HTML tags. It uses a callback function to perform replacements to simulate native lookahead support.</p>
<p>When is this sort of replacement suitable ?</p>
<ul>
<li>Remote HTML responses (ajax) highlighting</li>
<li>Autocompleters suggestion highlighting</li>
<li>User-typed HTML, WYSIWIGs, etc.</li>
</ul>
<p>Generally speaking, the only downside of this method, since it deals with the innerHTML, is that all attached events and properties are lost when the replacement is performed.</p>
<p><span id="more-725"></span></p>
<p>Note: this code is designed to match only beginning of words. If you want to match anywhere, remove <code class="inline">\\b</code> from the regular expression. And if you expect <code class="inline">&lt;script&gt;</code> tags, definitely don&#8217;t use it.</p>
<h3>MooTools version (<a href="/wp-content/articles/highlight-moo/">demo</a>)</h3>
<pre class='highlight ' lang="javascript">
Element.implement({

	highlight: function(search, insensitive, klass){
		var regex = new RegExp('(<[^>]*>)|(\\b'+ search.escapeRegExp() +')', insensitive ? 'ig' : 'g');
		return this.set('html', this.get('html').replace(regex, function(a, b, c){
			return (a.charAt(0) == '<') ? a : '<strong class="'+ klass +'">' + c + '</strong>';
		}));
	}

});
</pre>
<h3>jQuery version (<a href="/wp-content/articles/highlight-jquery/">demo</a>)</h3>
<pre class='highlight ' lang="javascript">
jQuery.fn.extend({

	highlight: function(search, insensitive, klass){
		var regex = new RegExp('(<[^>]*>)|(\\b'+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +')', insensitive ? 'ig' : 'g');
		return this.html(this.html().replace(regex, function(a, b, c){
			return (a.charAt(0) == '<') ? a : '<strong class="'+ klass +'">' + c + '</strong>';
		}));
	}

});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.devthought.com/2009/04/04/javascript-regexp-based-highlighting-function-for-mootools-and-jquery/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

