I am not sure if this question has been asked before but anyway, Consider these two examples:
h[x + h[y]] /. h[u_] :> g[u]
(*g[x + h[y]]*)
h[x + h[y]] /. h :> g
(*g[x + g[y]]*)
In the first one, the rules apply starting from outer level and once it applies then it stops. however, in the second example the rules apply up to the lowest level.
any explanation why there is different in the way ReplaceAll work.
thanks
ReplaceAlltraverses the expression in the same way in both your examples. In the first example the entire expression is replaced in the first match and traversal stops. (No traversal takes place on the substituted parts.) In the second exampleh[y]is not part of the expression that is replaced after the first match therefore it is exposed to additional traversal. – Mr.Wizard Oct 13 '14 at 06:44