This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.

Replace

Replace[expr, rules] applies a rule or list of rules in an attempt to transform the entire expression expr.

Replace[expr, rules, levelspec] applies rules to parts of expr specified by levelspec.

Examples: Replace[x^2, x^2 -> a] .

Replace[x + 1, x -> a] .

The rules must be of the form lhs -> rhs or lhs :> rhs.

A list of rules can be given. The rules are tried in order. The result of the first one that applies is returned. If none of the rules apply, the original expr is returned.

If the rules are given in nested lists, Replace is effectively mapped onto the inner lists. Thus Replace[expr, , , , ... , ... ] is equivalent to Replace[expr, , ], Replace[expr, , ... ], ... .

Delayed rules defined with :> can contain /; conditions.

Level specifications are described in Section A.3.6.

The default value for levelspec in Replace is {0}.

Replacements are performed to parts specified by levelspec even when those parts have Hold or related wrappers.