<?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: Multiple String Replacement Sugar</title>
	<atom:link href="http://blog.stevenlevithan.com/archives/multi-replace/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.stevenlevithan.com/archives/multi-replace</link>
	<description>A JavaScript and regular expression centric blog</description>
	<lastBuildDate>Wed, 08 Sep 2010 23:20:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: D Daniel</title>
		<link>http://blog.stevenlevithan.com/archives/multi-replace/comment-page-1#comment-35816</link>
		<dc:creator>D Daniel</dc:creator>
		<pubDate>Fri, 12 Jun 2009 20:32:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stevenlevithan.com/?p=78#comment-35816</guid>
		<description>Here is a version of multiReplace that will not overwrite a change if the change creates a new match later in the chain. The other multiReplace function&#039;s results are dependent on the order the replacements are listed in the hash array.

Also included is an example that will do php-like urlEncode on a string.


function mreplace(str,hash){
 var a = [];
 for(var key in hash){a[a.length] = key;}
 var regexp = a.join(&#039;&#124;&#039;);
 return str.replace( new RegExp(regexp,&#039;g&#039;), function(m,l){ return hash[m] &#124;&#124; hash[&quot;\\&quot;+m]; });
 }

function urlEncode(a){
 return mreplace(escape(a),{&#039;\\/&#039;:&#039;%2F&#039;,&#039;\\?&#039;:&#039;%3F&#039;,&#039;=&#039;:&#039;%3D&#039;,&#039;&amp;&#039;:&#039;%26&#039;,&#039;@&#039;:&#039;%40&#039;,&#039;\\s&#039;:&#039;+&#039;});
 }</description>
		<content:encoded><![CDATA[<p>Here is a version of multiReplace that will not overwrite a change if the change creates a new match later in the chain. The other multiReplace function&#8217;s results are dependent on the order the replacements are listed in the hash array.</p>
<p>Also included is an example that will do php-like urlEncode on a string.</p>
<p>function mreplace(str,hash){<br />
 var a = [];<br />
 for(var key in hash){a[a.length] = key;}<br />
 var regexp = a.join(&#8216;|&#8217;);<br />
 return str.replace( new RegExp(regexp,&#8217;g'), function(m,l){ return hash[m] || hash["\\"+m]; });<br />
 }</p>
<p>function urlEncode(a){<br />
 return mreplace(escape(a),{&#8216;\\/&#8217;:'%2F&#8217;,'\\?&#8217;:'%3F&#8217;,'=&#8217;:'%3D&#8217;,'&amp;&#8217;:'%26&#8242;,&#8217;@':&#8217;%40&#8242;,&#8217;\\s&#8217;:'+&#8217;});<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jim</title>
		<link>http://blog.stevenlevithan.com/archives/multi-replace/comment-page-1#comment-24937</link>
		<dc:creator>jim</dc:creator>
		<pubDate>Tue, 01 Jul 2008 19:59:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stevenlevithan.com/?p=78#comment-24937</guid>
		<description>This ought to be about the fastest, as it uses:
- only one regex call, a very simple split
- many non-regex string replace()
- one join()

â€“jim

&lt;pre class=&quot;code fixedHeight&quot;&gt;var html_ents = { &#039;#160&#039;:&#039; &#039;, &#039;#161&#039;:&#039;&#161;&#039;,&#039;#162&#039;:&#039;&#162;&#039;,&#039;#163&#039;:&#039;&#163;&#039;,&#039;#165&#039;:&#039;&#165;&#039;,
	&#039;#167&#039;:&#039;&#167;&#039;,&#039;#169&#039;:&#039;&#169;&#039;,&#039;#171&#039;:&#039;&#171;&#039;,&#039;#174&#039;:&#039;&#174;&#039;,&#039;#177&#039;:&#039;&#177;&#039;,&#039;#180&#039;:&#039;&#180;&#039;,
	&#039;#181&#039;:&#039;&#181;&#039;,&#039;#182&#039;:&#039;&#182;&#039;,&#039;#183&#039;:&#039;&#183;&#039;,&#039;#187&#039;:&#039;&#187;&#039;,&#039;#191&#039;:&#039;&#191;&#039;,&#039;#192&#039;:&#039;&#192;&#039;,
	&#039;#193&#039;:&#039;&#193;&#039;,&#039;#194&#039;:&#039;&#194;&#039;,&#039;#195&#039;:&#039;&#195;&#039;,&#039;#196&#039;:&#039;&#196;&#039;,&#039;#197&#039;:&#039;&#197;&#039;,&#039;#198&#039;:&#039;&#198;&#039;,
	&#039;#199&#039;:&#039;&#199;&#039;,&#039;#200&#039;:&#039;&#200;&#039;,&#039;#201&#039;:&#039;&#201;&#039;,&#039;#202&#039;:&#039;&#202;&#039;,&#039;#203&#039;:&#039;&#203;&#039;,&#039;#204&#039;:&#039;&#204;&#039;,
	&#039;#205&#039;:&#039;&#205;&#039;,&#039;#206&#039;:&#039;&#206;&#039;,&#039;#207&#039;:&#039;&#207;&#039;,&#039;#209&#039;:&#039;&#209;&#039;,&#039;#210&#039;:&#039;&#210;&#039;,&#039;#211&#039;:&#039;&#211;&#039;,
	&#039;#212&#039;:&#039;&#212;&#039;,&#039;#213&#039;:&#039;&#213;&#039;,&#039;#214&#039;:&#039;&#214;&#039;,&#039;#216&#039;:&#039;&#216;&#039;,&#039;#217&#039;:&#039;&#217;&#039;,&#039;#218&#039;:&#039;&#218;&#039;,
	&#039;#219&#039;:&#039;&#219;&#039;,&#039;#220&#039;:&#039;&#220;&#039;,&#039;#223&#039;:&#039;&#223;&#039;,&#039;#224&#039;:&#039;&#224;&#039;,&#039;#225&#039;:&#039;&#225;&#039;,&#039;#226&#039;:&#039;&#226;&#039;,
	&#039;#227&#039;:&#039;&#227;&#039;,&#039;#228&#039;:&#039;&#228;&#039;,&#039;#229&#039;:&#039;&#229;&#039;,&#039;#230&#039;:&#039;&#230;&#039;,&#039;#231&#039;:&#039;&#231;&#039;,&#039;#232&#039;:&#039;&#232;&#039;,
	&#039;#233&#039;:&#039;&#233;&#039;,&#039;#234&#039;:&#039;&#234;&#039;,&#039;#235&#039;:&#039;&#235;&#039;,&#039;#236&#039;:&#039;&#236;&#039;,&#039;#237&#039;:&#039;&#237;&#039;,&#039;#238&#039;:&#039;&#238;&#039;,
	&#039;#239&#039;:&#039;&#239;&#039;,&#039;#241&#039;:&#039;&#241;&#039;,&#039;#242&#039;:&#039;&#242;&#039;,&#039;#243&#039;:&#039;&#243;&#039;,&#039;#244&#039;:&#039;&#244;&#039;,&#039;#245&#039;:&#039;&#245;&#039;,
	&#039;#246&#039;:&#039;&#246;&#039;,&#039;#247&#039;:&#039;&#247;&#039;,&#039;#248&#039;:&#039;&#248;&#039;,&#039;#249&#039;:&#039;&#249;&#039;,&#039;#250&#039;:&#039;&#250;&#039;,&#039;#251&#039;:&#039;&#251;&#039;,
	&#039;#252&#039;:&#039;&#252;&#039;,&#039;#255&#039;:&#039;&#255;&#039;,&#039;#34&#039;:&#039;&#039;,&#039;#38&#039;:&#039;&amp;&#039;,&#039;#60&#039;:&#039;&lt;&#039;,&#039;#62&#039;:&#039;&gt;&#039;,
	&#039;#8211&#039;:&#039;â€“&#039;,&#039;#8212&#039;:&#039;â€”&#039;,&#039;#8364&#039;:&#039;â‚¬&#039;,&#039;#96&#039;:&#039;`&#039;,
	&#039;aacute&#039;:&#039;&#193;&#039;,&#039;aacute&#039;:&#039;&#225;&#039;,&#039;acirc&#039;:&#039;&#194;&#039;,&#039;acirc&#039;:&#039;&#226;&#039;,&#039;aelig&#039;:&#039;&#198;&#039;,
	&#039;aelig&#039;:&#039;&#230;&#039;,&#039;agrave&#039;:&#039;&#192;&#039;,&#039;agrave&#039;:&#039;&#224;&#039;,&#039;amp&#039;:&#039;&amp;&#039;,&#039;aring&#039;:&#039;&#197;&#039;,
	&#039;aring&#039;:&#039;&#229;&#039;,&#039;atilde&#039;:&#039;&#195;&#039;,&#039;atilde&#039;:&#039;&#227;&#039;,&#039;auml&#039;:&#039;&#196;&#039;,&#039;auml&#039;:&#039;&#228;&#039;,
	&#039;ccedil&#039;:&#039;&#199;&#039;,&#039;ccedil&#039;:&#039;&#231;&#039;,&#039;cent&#039;:&#039;&#162;&#039;,&#039;copy&#039;:&#039;&#169;&#039;,&#039;divide&#039;:&#039;&#247;&#039;,
	&#039;eacute&#039;:&#039;&#201;&#039;,&#039;eacute&#039;:&#039;&#233;&#039;,&#039;ecirc&#039;:&#039;&#202;&#039;,&#039;ecirc&#039;:&#039;&#234;&#039;,&#039;egrave&#039;:&#039;&#200;&#039;,
	&#039;egrave&#039;:&#039;&#232;&#039;,&#039;euml&#039;:&#039;&#203;&#039;,&#039;euml&#039;:&#039;&#235;&#039;,&#039;euro&#039;:&#039;â‚¬&#039;,&#039;gt&#039;:&#039;&gt;&#039;,
	&#039;iacute&#039;:&#039;&#205;&#039;,&#039;iacute&#039;:&#039;&#237;&#039;,&#039;icirc&#039;:&#039;&#206;&#039;,&#039;icirc&#039;:&#039;&#238;&#039;,&#039;iexcl&#039;:&#039;&#161;&#039;,
	&#039;igrave&#039;:&#039;&#204;&#039;,&#039;igrave&#039;:&#039;&#236;&#039;,&#039;iquest&#039;:&#039;&#191;&#039;,&#039;iuml&#039;:&#039;&#207;&#039;,&#039;iuml&#039;:&#039;&#239;&#039;,
	&#039;laquo&#039;:&#039;&#171;&#039;,&#039;lt&#039;:&#039;&lt;&#039;,&#039;mdash&#039;:&#039;â€”&#039;,&#039;micro&#039;:&#039;&#181;&#039;,&#039;middot&#039;:&#039;&#183;&#039;,
	&#039;nbsp&#039;:&#039; &#039;,&#039;ndash&#039;:&#039;â€“&#039;,&#039;ntilde&#039;:&#039;&#209;&#039;,&#039;ntilde&#039;:&#039;&#241;&#039;,&#039;oacute&#039;:&#039;&#211;&#039;,
	&#039;oacute&#039;:&#039;&#243;&#039;,&#039;ocirc&#039;:&#039;&#212;&#039;,&#039;ocirc&#039;:&#039;&#244;&#039;,&#039;ograve&#039;:&#039;&#210;&#039;,&#039;ograve&#039;:&#039;&#242;&#039;,
	&#039;oslash&#039;:&#039;&#216;&#039;,&#039;oslash&#039;:&#039;&#248;&#039;,&#039;otilde&#039;:&#039;&#213;&#039;,&#039;otilde&#039;:&#039;&#245;&#039;,&#039;ouml&#039;:&#039;&#214;&#039;,
	&#039;ouml&#039;:&#039;&#246;&#039;,&#039;para&#039;:&#039;&#182;&#039;,&#039;plusmn&#039;:&#039;&#177;&#039;,&#039;pound&#039;:&#039;&#163;&#039;,&#039;quot&#039;:&#039;\&quot;&#039;,
	&#039;raquo&#039;:&#039;&#187;&#039;,&#039;reg&#039;:&#039;&#174;&#039;,&#039;sect&#039;:&#039;&#167;&#039;,&#039;szlig&#039;:&#039;&#223;&#039;,&#039;uacute&#039;:&#039;&#218;&#039;,
	&#039;uacute&#039;:&#039;&#250;&#039;,&#039;ucirc&#039;:&#039;&#219;&#039;,&#039;ucirc&#039;:&#039;&#251;&#039;,&#039;ugrave&#039;:&#039;&#217;&#039;,&#039;ugrave&#039;:&#039;&#249;&#039;,
	&#039;uuml&#039;:&#039;&#220;&#039;,&#039;uuml&#039;:&#039;&#252;&#039;,&#039;yen&#039;:&#039;&#165;&#039;,&#039;yuml&#039;:&#039;&#255;&#039;};

function html_entity_decode(str) {

	parts = str.split(/&amp;&#124;;/g);

	for(i=1, stop=parts.length; i&lt;stop; i+=2) {
		parts[i] = html_ents[parts[i]];
	}

	return parts.join(&#039;&#039;);
}

function init() {

	document.writeln(&#039;some &lt;b&gt;bold&lt;/b&gt; text&#039;);
	document.writeln(&#039;&lt;br&gt;&lt;br&gt;

&#039;);
	document.writeln(&#039;some &lt;b&gt;bold&lt;/b&gt; text&#039;.split(/&amp;&#124;;/));
	document.writeln(&#039;&lt;br&gt;&lt;br&gt;
&#039;);
	document.writeln(html_entity_decode(&#039;some &lt;b&gt;bold&lt;/b&gt; text&#039;));
	document.writeln(&#039;&lt;br&gt;&lt;br&gt;

&#039;);
	document.writeln(html_entity_decode(&#039;&lt;b&gt;bold&lt;/b&gt; text&#039;));
}

init();&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>This ought to be about the fastest, as it uses:<br />
- only one regex call, a very simple split<br />
- many non-regex string replace()<br />
- one join()</p>
<p>â€“jim</p>
<pre class="code fixedHeight">var html_ents = { '#160':' ', '#161':'&iexcl;','#162':'&cent;','#163':'&pound;','#165':'&yen;',
	'#167':'&sect;','#169':'&copy;','#171':'&laquo;','#174':'&reg;','#177':'&plusmn;','#180':'&acute;',
	'#181':'&micro;','#182':'&para;','#183':'&middot;','#187':'&raquo;','#191':'&iquest;','#192':'&Agrave;',
	'#193':'&Aacute;','#194':'&Acirc;','#195':'&Atilde;','#196':'&Auml;','#197':'&Aring;','#198':'&AElig;',
	'#199':'&Ccedil;','#200':'&Egrave;','#201':'&Eacute;','#202':'&Ecirc;','#203':'&Euml;','#204':'&Igrave;',
	'#205':'&Iacute;','#206':'&Icirc;','#207':'&Iuml;','#209':'&Ntilde;','#210':'&Ograve;','#211':'&Oacute;',
	'#212':'&Ocirc;','#213':'&Otilde;','#214':'&Ouml;','#216':'&Oslash;','#217':'&Ugrave;','#218':'&Uacute;',
	'#219':'&Ucirc;','#220':'&Uuml;','#223':'&szlig;','#224':'&agrave;','#225':'&aacute;','#226':'&acirc;',
	'#227':'&atilde;','#228':'&auml;','#229':'&aring;','#230':'&aelig;','#231':'&ccedil;','#232':'&egrave;',
	'#233':'&eacute;','#234':'&ecirc;','#235':'&euml;','#236':'&igrave;','#237':'&iacute;','#238':'&icirc;',
	'#239':'&iuml;','#241':'&ntilde;','#242':'&ograve;','#243':'&oacute;','#244':'&ocirc;','#245':'&otilde;',
	'#246':'&ouml;','#247':'&divide;','#248':'&oslash;','#249':'&ugrave;','#250':'&uacute;','#251':'&ucirc;',
	'#252':'&uuml;','#255':'&yuml;','#34':'','#38':'&amp;','#60':'&lt;','#62':'&gt;',
	'#8211':'â€“','#8212':'â€”','#8364':'â‚¬','#96':'`',
	'aacute':'&Aacute;','aacute':'&aacute;','acirc':'&Acirc;','acirc':'&acirc;','aelig':'&AElig;',
	'aelig':'&aelig;','agrave':'&Agrave;','agrave':'&agrave;','amp':'&amp;','aring':'&Aring;',
	'aring':'&aring;','atilde':'&Atilde;','atilde':'&atilde;','auml':'&Auml;','auml':'&auml;',
	'ccedil':'&Ccedil;','ccedil':'&ccedil;','cent':'&cent;','copy':'&copy;','divide':'&divide;',
	'eacute':'&Eacute;','eacute':'&eacute;','ecirc':'&Ecirc;','ecirc':'&ecirc;','egrave':'&Egrave;',
	'egrave':'&egrave;','euml':'&Euml;','euml':'&euml;','euro':'â‚¬','gt':'&gt;',
	'iacute':'&Iacute;','iacute':'&iacute;','icirc':'&Icirc;','icirc':'&icirc;','iexcl':'&iexcl;',
	'igrave':'&Igrave;','igrave':'&igrave;','iquest':'&iquest;','iuml':'&Iuml;','iuml':'&iuml;',
	'laquo':'&laquo;','lt':'&lt;','mdash':'â€”','micro':'&micro;','middot':'&middot;',
	'nbsp':' ','ndash':'â€“','ntilde':'&Ntilde;','ntilde':'&ntilde;','oacute':'&Oacute;',
	'oacute':'&oacute;','ocirc':'&Ocirc;','ocirc':'&ocirc;','ograve':'&Ograve;','ograve':'&ograve;',
	'oslash':'&Oslash;','oslash':'&oslash;','otilde':'&Otilde;','otilde':'&otilde;','ouml':'&Ouml;',
	'ouml':'&ouml;','para':'&para;','plusmn':'&plusmn;','pound':'&pound;','quot':'\&amp;quot;',
	'raquo':'&raquo;','reg':'&reg;','sect':'&sect;','szlig':'&szlig;','uacute':'&Uacute;',
	'uacute':'&uacute;','ucirc':'&Ucirc;','ucirc':'&ucirc;','ugrave':'&Ugrave;','ugrave':'&ugrave;',
	'uuml':'&Uuml;','uuml':'&uuml;','yen':'&yen;','yuml':'&yuml;'};

function html_entity_decode(str) {

	parts = str.split(/&amp;|;/g);

	for(i=1, stop=parts.length; i&lt;stop; i+=2) {
		parts[i] = html_ents[parts[i]];
	}

	return parts.join('');
}

function init() {

	document.writeln('some &amp;lt;b&amp;gt;bold&amp;lt;/b&amp;gt; text');
	document.writeln('&lt;br&gt;&lt;br&gt;

');
	document.writeln('some &amp;lt;b&amp;gt;bold&amp;lt;/b&amp;gt; text'.split(/&amp;|;/));
	document.writeln('&lt;br&gt;&lt;br&gt;
');
	document.writeln(html_entity_decode('some &amp;lt;b&amp;gt;bold&amp;lt;/b&amp;gt; text'));
	document.writeln('&lt;br&gt;&lt;br&gt;

');
	document.writeln(html_entity_decode('&amp;lt;b&amp;gt;bold&amp;lt;/b&amp;gt; text'));
}

init();</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julian Turner</title>
		<link>http://blog.stevenlevithan.com/archives/multi-replace/comment-page-1#comment-24898</link>
		<dc:creator>Julian Turner</dc:creator>
		<pubDate>Sat, 21 Jun 2008 11:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stevenlevithan.com/?p=78#comment-24898</guid>
		<description>3 extra characters - oh no, code bloat! 

No, I appreciate this is a bit of play.

It tried experimenting with doing a &quot;RegExp.prototype.test()&quot; before the &quot;replace()&quot; to see if that gave any performance improvements, by saving unnecessary &quot;str = str.replace&quot;.  No, it was worse!

As operations on immutable things such a strings, I believe, can be very efficient, the str = str.replace, even for a lot of replacements, is very performant, so there does not seem to be any reason to optimise further, for the general case.</description>
		<content:encoded><![CDATA[<p>3 extra characters &#8211; oh no, code bloat! </p>
<p>No, I appreciate this is a bit of play.</p>
<p>It tried experimenting with doing a &#8220;RegExp.prototype.test()&#8221; before the &#8220;replace()&#8221; to see if that gave any performance improvements, by saving unnecessary &#8220;str = str.replace&#8221;.  No, it was worse!</p>
<p>As operations on immutable things such a strings, I believe, can be very efficient, the str = str.replace, even for a lot of replacements, is very performant, so there does not seem to be any reason to optimise further, for the general case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Levithan</title>
		<link>http://blog.stevenlevithan.com/archives/multi-replace/comment-page-1#comment-24744</link>
		<dc:creator>Steven Levithan</dc:creator>
		<pubDate>Fri, 20 Jun 2008 17:05:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stevenlevithan.com/?p=78#comment-24744</guid>
		<description>@Julian Turner, but that takes three extra characters per replacement. :-P

Kidding aside, I agree that simple/flexible is better. I was mostly just experimenting and playing here, especially since the standard replacement syntax (shown at the beginning of this post) isn&#039;t that bad to begin with.</description>
		<content:encoded><![CDATA[<p>@Julian Turner, but that takes three extra characters per replacement. <img src='http://blog.stevenlevithan.com/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> </p>
<p>Kidding aside, I agree that simple/flexible is better. I was mostly just experimenting and playing here, especially since the standard replacement syntax (shown at the beginning of this post) isn&#8217;t that bad to begin with.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julian Turner</title>
		<link>http://blog.stevenlevithan.com/archives/multi-replace/comment-page-1#comment-24740</link>
		<dc:creator>Julian Turner</dc:creator>
		<pubDate>Fri, 20 Jun 2008 16:28:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.stevenlevithan.com/?p=78#comment-24740</guid>
		<description>If you want extra flexibility, why not use an array:-

&lt;pre class=&quot;code&quot;&gt;var replacements = [
    [/a/g, &quot;b&quot;],
    [/c/g, &quot;d&quot;],
]

String.prototype.multiReplace = function ( replacements ) {
	var str = this, i;
	for (i = 0; i &lt; replacements.length; i++ ) {
		str = str.replace(replacements[i][0], replacements[i][1]);
	}
	return str;
};&lt;/pre&gt;

This guarantees the order, saves having to generate the RegExp each time, and lets you use different switches.</description>
		<content:encoded><![CDATA[<p>If you want extra flexibility, why not use an array:-</p>
<pre class="code">var replacements = [
    [/a/g, "b"],
    [/c/g, "d"],
]

String.prototype.multiReplace = function ( replacements ) {
	var str = this, i;
	for (i = 0; i &lt; replacements.length; i++ ) {
		str = str.replace(replacements[i][0], replacements[i][1]);
	}
	return str;
};</pre>
<p>This guarantees the order, saves having to generate the RegExp each time, and lets you use different switches.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
