<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://regexadvice.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Construction Advice</title><link>http://regexadvice.com/forums/68/ShowForum.aspx</link><description>&lt;P&gt;Find help building expressions to suit your needs. Consult the &lt;A href="http://regexlib.com"&gt;Regular Expression Library&lt;/A&gt; to find many pre-built expressions, and add your own once you've solved your construction problem in our forum.&lt;/P&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2.1 (Build: 60809.935)</generator><item><title>Re: Performing multiple replaces in iteration</title><link>http://regexadvice.com/forums/thread/85852.aspx</link><pubDate>Tue, 24 Jul 2012 01:36:14 GMT</pubDate><guid isPermaLink="false">d291b357-6366-4006-9008-4266c301325a:85852</guid><dc:creator>Aussie Susan</dc:creator><slash:comments>0</slash:comments><comments>http://regexadvice.com/forums/thread/85852.aspx</comments><wfw:commentRss>http://regexadvice.com/forums/commentrss.aspx?SectionID=68&amp;PostID=85852</wfw:commentRss><description>&lt;p&gt;From what I understand about the JavaScript regex functions, the &amp;quot;replace()&amp;quot; function can take either a string (as you are passing) or a regex pattern.&lt;/p&gt;&lt;p&gt;I *think* what you want to use is:&lt;/p&gt;&lt;p&gt;src = src.replace(/ab/g, &amp;quot;($0)&amp;quot;);&lt;/p&gt;&lt;p&gt;The &amp;quot;/g&amp;quot; tells the regex engine to perform a &amp;quot;G&amp;quot;lobal replace which means that it will do the iterations for you. Also the &amp;#39;$0&amp;#39; is a back-reference to the entire matched text - in effect the same as the &amp;#39;arr[0]&amp;#39; i your program.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I don&amp;#39;t know enough about the JavaScript regex functions specifically, but I do know that there is no such thing as a &amp;quot;common&amp;quot; regex in terms of pattern syntax or capabilities of the regex engine. I suspect that the &amp;quot;last index&amp;quot; that you were referring to IS used by some regex implementations to let the user define a starting point (the .NET regex replace() function has a version with 4 parameters, the last one being where to start the search in the text string) and there are all sorts of ways that this is implemented. However the regex engine used in JavaScript seems to be fairly limited in its capabilities (in this regard at least) and therefore you need to use the technique I mentioned above.&lt;/p&gt;&lt;p&gt;Susan &lt;br /&gt;&lt;/p&gt;</description></item><item><title>Performing multiple replaces in iteration</title><link>http://regexadvice.com/forums/thread/85850.aspx</link><pubDate>Mon, 23 Jul 2012 18:57:12 GMT</pubDate><guid isPermaLink="false">d291b357-6366-4006-9008-4266c301325a:85850</guid><dc:creator>gman</dc:creator><slash:comments>0</slash:comments><comments>http://regexadvice.com/forums/thread/85850.aspx</comments><wfw:commentRss>http://regexadvice.com/forums/commentrss.aspx?SectionID=68&amp;PostID=85850</wfw:commentRss><description>&lt;p&gt;I&amp;#39;m working&amp;nbsp;with JS on Kettle and am trying to create logic which will search regex patterns and enclose these captured strings&amp;nbsp;in&amp;nbsp;parentheses for one to many&amp;nbsp;occurrences. For example, in the string &amp;#39;ABCDABCABD&amp;#39; the desired result would be &amp;#39;(AB)CD(AB)C(AB)D&amp;#39; with a regex of /AB/g.&lt;/p&gt;&lt;p&gt;I have tried using this logic without any success because&amp;nbsp;it ends up in a loop&amp;nbsp;replacing the first occurrence over and over.&amp;nbsp;From google searchs, I was under the assumption that lastIndex would set the starting point for the next iteration but that is not happening.&lt;/p&gt;&lt;p&gt;&amp;nbsp;var src = &amp;quot;ABCDABCABD&amp;quot;;&lt;br /&gt;&amp;nbsp;var arr;&lt;br /&gt;&amp;nbsp;var re = /AB/g;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;while ( (arr = re.exec(src)) != null)&lt;br /&gt;&amp;nbsp;&amp;nbsp; { src = src.replace(arr[0], &amp;quot;(&amp;quot; + arr[0] + &amp;quot;)&amp;quot; );&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;Can someone please offer a suggestion on how to handle situations like this?&lt;/p&gt;&lt;p&gt;TIA&lt;/p&gt;&lt;p&gt;Gerald&lt;br /&gt;&lt;/p&gt;</description></item></channel></rss>