function example1(X) {
newVar = X.replace(/St(ring)/gi, "e;Prepend "e;+"e;$1"e;); //Should output "e;Prepend ring"e; and does! :-)
return newVar;
}
function example2(X) {
newVar = X.replace(/Some(String)/gi, example1("e;$1"e;)); //Should also output "e;Prepend ring"e; but doesn't! :-(
return newVar;
}
Any idea why the second function above fails? It only seems to fail if there's a search/replace in the function it calls. It just outputs "String" (The replace() in example1() doesn't replace anything. Test it and see
Javascript, like C, evaluates all dependant expressions first. In this case, example2() creates a new regexp object implicitly, and calls example1 with '$1' as an argument first.