<?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: Remove Nested Patterns with One Line of JavaScript</title>
	<atom:link href="http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Thu, 09 Feb 2012 06:55:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: Stefan</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264390</link>
		<dc:creator>Stefan</dc:creator>
		<pubDate>Wed, 04 Jun 2008 11:08:50 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264390</guid>
		<description>I like your concept a lot, as it seems to perform better than my similar solution. 

One bug though. 

The result of &quot;(1(ab)2(cd)3)&quot; must be the input string itself. But your second pattern yields [(ab), 1(cd)2].

Thanks for sharing.</description>
		<content:encoded><![CDATA[<p>I like your concept a lot, as it seems to perform better than my similar solution. </p>
<p>One bug though. </p>
<p>The result of &#8220;(1(ab)2(cd)3)&#8221; must be the input string itself. But your second pattern yields [(ab), 1(cd)2].</p>
<p>Thanks for sharing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: icoloma</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264354</link>
		<dc:creator>icoloma</dc:creator>
		<pubDate>Tue, 03 Jun 2008 12:07:56 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264354</guid>
		<description>jentulman, if you try to sanitize some third-party HTML code and remove all &lt;script&gt; tags, you may find that someone feeds your system &lt;scri&lt;script&gt;pt&gt;, where you would get rid of one of them but not both. I suppose this fix addresses this.</description>
		<content:encoded><![CDATA[<p>jentulman, if you try to sanitize some third-party HTML code and remove all &lt;script&gt; tags, you may find that someone feeds your system &lt;scri&lt;script&gt;pt&gt;, where you would get rid of one of them but not both. I suppose this fix addresses this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jentulman</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264329</link>
		<dc:creator>jentulman</dc:creator>
		<pubDate>Mon, 02 Jun 2008 08:36:19 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264329</guid>
		<description>Hope you&#039;ll excuse a newbish question, but could someone spread a little light on the why and where of this.
I&#039;ve not heard of them before and a google for &#039;nested patterns&#039; tends to bring me back here.</description>
		<content:encoded><![CDATA[<p>Hope you&#8217;ll excuse a newbish question, but could someone spread a little light on the why and where of this.<br />
I&#8217;ve not heard of them before and a google for &#8216;nested patterns&#8217; tends to bring me back here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bluesmoon</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264328</link>
		<dc:creator>bluesmoon</dc:creator>
		<pubDate>Mon, 02 Jun 2008 07:07:27 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264328</guid>
		<description>maybe I&#039;m missing something, but isn&#039;t this common knowledge?</description>
		<content:encoded><![CDATA[<p>maybe I&#8217;m missing something, but isn&#8217;t this common knowledge?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Levithan</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264315</link>
		<dc:creator>Steven Levithan</dc:creator>
		<pubDate>Fri, 30 May 2008 22:59:42 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264315</guid>
		<description>@Nosredna, I&#039;m not sure what the killTags function would buy you vs. a simple `str = str.replace(/&lt;[^&gt;]*&gt;/g, &quot;&quot;)`. Although the output would be different in edge cases, it wouldn&#039;t really be any better or worse. For the most part, both would just remove all tags, and leave content within them alone.

However, this trick could indeed be used for some nested HTML handling. Let&#039;s say you wanted to remove all div tags and their contents, accounting for nested and self-closed divs. (And for some reason you didn&#039;t want to use the DOM to help.)

while (str != (str = str.replace(/&lt;div\b[^&gt;]*?\/&gt;&#124;&lt;div\b[^&gt;]*&gt;(?:(?!&lt;div\b[^&gt;]*&gt;&#124;&lt;\/div&gt;)[\S\s])*&lt;\/div&gt;/gi, &quot;&quot;)));

Done.

&gt; how would it be fixed to handle things like that which collide with the
&gt; regex interpretation? Is there an easy way to escape the delimiters?

Here you go:

function escapeRegExp (str) {
&#160;&#160;&#160;&#160;return str.replace(/[-[\]{}()*+?.\\^$&#124;,]/g, &quot;\\$&amp;&quot;);
}

My &lt;a href=&quot;http://stevenlevithan.com/regex/xregexp/&quot; rel=&quot;nofollow&quot;&gt;XRegExp library&lt;/a&gt; provides something like this as XRegExp.escape, except that it also escapes &quot;#&quot; and whitespace because of its support for free-spacing/extended mode.</description>
		<content:encoded><![CDATA[<p>@Nosredna, I&#8217;m not sure what the killTags function would buy you vs. a simple `str = str.replace(/&lt;[^&gt;]*&gt;/g, &#8220;&#8221;)`. Although the output would be different in edge cases, it wouldn&#8217;t really be any better or worse. For the most part, both would just remove all tags, and leave content within them alone.</p>
<p>However, this trick could indeed be used for some nested HTML handling. Let&#8217;s say you wanted to remove all div tags and their contents, accounting for nested and self-closed divs. (And for some reason you didn&#8217;t want to use the DOM to help.)</p>
<p>while (str != (str = str.replace(/&lt;div\b[^&gt;]*?\/&gt;|&lt;div\b[^&gt;]*&gt;(?:(?!&lt;div\b[^&gt;]*&gt;|&lt;\/div&gt;)[\S\s])*&lt;\/div&gt;/gi, &#8220;&#8221;)));</p>
<p>Done.</p>
<p>&gt; how would it be fixed to handle things like that which collide with the<br />
&gt; regex interpretation? Is there an easy way to escape the delimiters?</p>
<p>Here you go:</p>
<p>function escapeRegExp (str) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;return str.replace(/[-[\]{}()*+?.\\^$|,]/g, &#8220;\\$&amp;&#8221;);<br />
}</p>
<p>My <a href="http://stevenlevithan.com/regex/xregexp/" rel="nofollow">XRegExp library</a> provides something like this as XRegExp.escape, except that it also escapes &#8220;#&#8221; and whitespace because of its support for free-spacing/extended mode.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nosredna</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264208</link>
		<dc:creator>Nosredna</dc:creator>
		<pubDate>Fri, 30 May 2008 19:06:44 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264208</guid>
		<description>Heh. I don&#039;t think the &quot;code&quot; tag works quite right here at ajaxian.

	//	Parameters:
	//		ld - left delimiter 
	//		rd - right delimiter 
	//		str - string to be stripped</description>
		<content:encoded><![CDATA[<p>Heh. I don&#8217;t think the &#8220;code&#8221; tag works quite right here at ajaxian.</p>
<p>	//	Parameters:<br />
	//		ld &#8211; left delimiter<br />
	//		rd &#8211; right delimiter<br />
	//		str &#8211; string to be stripped</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nosredna</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264207</link>
		<dc:creator>Nosredna</dc:creator>
		<pubDate>Fri, 30 May 2008 19:05:06 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264207</guid>
		<description>A function based on this trick that lets you specify the delimiters.

Problem: I&#039;m not a regex hero, so while it works with braces and angle brackets, it fails on parentheses. So how would it be fixed to handle things like that which collide with the regex interpretation? Is there an easy way to escape the delimiters?

&lt;code&gt;
	//Function: killTags
	//	Removes nested tags
	//
	//	Parameters:
	//		ld - left delimiter (for example: )
	//		str - string to be stripped
	//
	//	Returns:
	//		str - stripped string		
	function killTags(ld,rd,str) {
		while (str!=(str=str.replace(new RegExp(ld+&quot;[^&quot;+ld+rd+&quot;]*&quot;+rd),&quot;g&quot;)));
		return str;
	};
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>A function based on this trick that lets you specify the delimiters.</p>
<p>Problem: I&#8217;m not a regex hero, so while it works with braces and angle brackets, it fails on parentheses. So how would it be fixed to handle things like that which collide with the regex interpretation? Is there an easy way to escape the delimiters?</p>
<p><code><br />
	//Function: killTags<br />
	//	Removes nested tags<br />
	//<br />
	//	Parameters:<br />
	//		ld - left delimiter (for example: )<br />
	//		str - string to be stripped<br />
	//<br />
	//	Returns:<br />
	//		str - stripped string<br />
	function killTags(ld,rd,str) {<br />
		while (str!=(str=str.replace(new RegExp(ld+"[^"+ld+rd+"]*"+rd),"g")));<br />
		return str;<br />
	};<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: starkraving</title>
		<link>http://ajaxian.com/archives/remove-nested-patterns-with-one-line-of-javascript/comment-page-1#comment-264206</link>
		<dc:creator>starkraving</dc:creator>
		<pubDate>Fri, 30 May 2008 15:09:34 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/?p=3701#comment-264206</guid>
		<description>This is actually quite useful. It could be modified slightly to make a trim() function, or ltrim() or rtrim(). Cool.</description>
		<content:encoded><![CDATA[<p>This is actually quite useful. It could be modified slightly to make a trim() function, or ltrim() or rtrim(). Cool.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

