{"id":6333,"date":"2012-08-24T17:00:42","date_gmt":"2012-08-24T11:30:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6333"},"modified":"2017-09-22T15:32:00","modified_gmt":"2017-09-22T10:02:00","slug":"replaceall-jquery-regex","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/replaceall-jquery-regex\/","title":{"rendered":"Replace All using JQuery and Regex"},"content":{"rendered":"<p>I recently found out that use of Regex with jQuery and replace function can turn out to be a very powerful tool for replacing\/loading data in html dynamically<\/p>\n<p>The use of replace is made like<\/p>\n<p>[js]<br \/>\nvar a = &quot;Some text here text&quot;;<br \/>\na=a.replace(\/text\/g,&quot;one&quot;)<br \/>\n[\/js]<\/p>\n<p>This will replace all &#8220;text&#8221; with &#8220;one&#8221;<\/p>\n<p>Now imagine how good it would be if we could replace the part of html like &#8220;##title##&#8221; with a title&#8217;s value in a JSON.<br \/>\nWe would be able to replace\/load an entire html code dynamically by merely sending a JSON (or a list of JSON) that would replace the content of HTML in javascript.<\/p>\n<p>Let us consider that we have a html code which is somethinng like:<\/p>\n<p>[html]<br \/>\n&lt;a title=&#8217;##title##&#8217; onclick=&#8217;showRecipeDetail(\\&quot;##friendlyUrl##\\&quot;, this)&#8217;&gt; &lt;img src=\\&quot;##imageUrl##\\&quot; height=&#8217;75&#8217; width=&#8217;75&#8217; \/&gt;&lt;div class=&#8217;recipeTitle&#8217;&gt;##title##&lt;\/div&gt;&lt;\/a&gt;<br \/>\n[\/html]<\/p>\n<p>And we want to load this html with JSON content:<\/p>\n<p>[js]<br \/>\n{{title:&quot;abc&quot;, friendlyUrl:&quot;xyz&quot;, imageUrl:&quot;123&quot;},{title:&quot;pqr&quot;, friendlyUrl:&quot;stu&quot;, imageUrl:&quot;456&quot;}}<br \/>\n[\/js]<\/p>\n<p>What we will do is that we will identified the values to be replaced by using regex and then replaced all of them using replace function. For eg.:<\/p>\n<p>[js]<br \/>\nvar data= [{title:&quot;abc&quot;, friendlyUrl:&quot;xyz&quot;, imageUrl:&quot;123&quot;},{title:&quot;pqr&quot;, friendlyUrl:&quot;stu&quot;, imageUrl:&quot;456&quot;}]<br \/>\nvar HTML_Template = &quot;&lt;a title=&#8217;##title##&#8217; onclick=&#8217;showRecipeDetail(\\&quot;##friendlyUrl##\\&quot;, this)&#8217;&gt; &lt;img src=\\&quot;##imageUrl##\\&quot; height=&#8217;75&#8217; width=&#8217;75&#8217; \/&gt;&lt;div class=&#8217;recipeTitle&#8217;&gt;##title##&lt;\/div&gt;&lt;\/a&gt;&quot;;<br \/>\njQuery(data).each(function(index, recipe) {<br \/>\n\t\t\tvar html = HTML_Template;<br \/>\n\t\t\tjQuery.each(recipe, function(key, value){<br \/>\n\t\t\t\tvar regExp = new RegExp(&quot;##&quot; + key + &quot;##&quot;, &#8216;g&#8217;);<br \/>\n\t\t\t\thtml = html.replace(regExp, value)<br \/>\n\t\t\t});<br \/>\n        });<br \/>\n[\/js]<\/p>\n<p>And hence we will have a our html replaced by merely few line of javascript code and a list of JSON (which we might be getting from ajax responce).<\/p>\n<p>Hope this blog was helpful to you.. \ud83d\ude42<\/p>\n<p>Thanks and Regard,<br \/>\nGaurav Sharma<br \/>\n(gauravs@intelligrape.com)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently found out that use of Regex with jQuery and replace function can turn out to be a very powerful tool for replacing\/loading data in html dynamically The use of replace is made like [js] var a = &quot;Some text here text&quot;; a=a.replace(\/text\/g,&quot;one&quot;) [\/js] This will replace all &#8220;text&#8221; with &#8220;one&#8221; Now imagine how [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":44},"categories":[1],"tags":[4842,27,431,875,876],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6333"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6333"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6333\/revisions"}],"predecessor-version":[{"id":51922,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6333\/revisions\/51922"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}