Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Back Reference Overwrites Changes

  •  11-20-2007, 4:15 PM

    Back Reference Overwrites Changes

    I ran into an interesting situation where back references overwrite previous changes in a regular expression.

    I needed a regular expression that updated a pattern but also checked for something that comes before the pattern.  It should leave everything before the pattern as it was before the updates.  There can be multiple matches of the pattern to replace.  I expected replacements made by the regular expression to be visible in future back references but it doesn't seem to be the case. 

    Only the last matched pattern is updated.  (Actually i think all matches are updated, the first updates just seem to be overwritten.)

    Is there a way to make these previous updates show up in the future back references? What is the correct way to approach this problem?
     

    Language: JavaScript

    Example Function:

            function replaceTest() {
                var reg, str;
                var test_str = "istriiiiiingi";
                str = "([\\w]+)(i)([\\w]*)"    // find the i character where there is at least one preceding character.
                reg = new RegExp( str, "ig" );     
                test_str = test_str.replace(reg, "$1a$3")
                alert(test_str);    //result is "istriiiiiinga", how do we make this "istraaaaaanga"
                return false;
            }

     

     

View Complete Thread