expr//.rules
repeatedly performs replacements until expr no longer changes.
ReplaceRepeated[rules]
represents an operator form of ReplaceRepeated that can be applied to an expression.
ReplaceRepeated 
expr//.rules
repeatedly performs replacements until expr no longer changes.
ReplaceRepeated[rules]
represents an operator form of ReplaceRepeated that can be applied to an expression.
Details and Options
- expr//.rules effectively applies /. repeatedly, until the results it gets no longer change.
- It performs one complete pass over the expression using /., then carries out the next pass.
- You should be very careful to avoid infinite loops when you use the //. operator. The command x//.x->x+1 will, for example, lead to an infinite loop.
- ReplaceRepeated takes the option MaxIterations, which specifies the maximum number of times it will try to apply the rules you give. The default setting is MaxIterations->65536. With MaxIterations->Infinity there is no limit.
- ReplaceRepeated[rules][expr] is equivalent to ReplaceRepeated[expr,rules].
Examples
open all close allBasic Examples (3)
Apply rules for the power and product laws for logarithms of real numbers recursively:
rules = {Log[x_ y_] :> Log[x] + Log[y], Log[x_ ^ k_] :> k Log[x]};Log[Sqrt[a(b c ^ d) ^ e]] //. rulesReplaceAll does just a single replacement:
Log[Sqrt[a (b c ^ d) ^ e]] /. rulesRemove all occurrences of a head, including nested ones:
{f[f[x]], f[x], g[f[x]], f[g[f[x]]]} //. f[x_] -> xReplaceAll would only remove the outermost head:
{f[f[x]], f[x], g[f[x]], f[g[f[x]]]} /. f[x_] -> xUse ReplaceRepeated in operator form:
ReplaceRepeated[f[x_] -> x][{f[f[x]], f[g[f[x]]]}]Scope (3)
If no rule matches, the input expression is returned:
x //. {y -> 2, z -> 3}Replace occurrences of f with 1 followed by exactly one integer:
{f[1], f[1, f[1, 2]], f[1, f[1, 2, 3]]} //. f[1, x_Integer] :> 1Replace occurrences of f with 1 followed by one or more integers:
{f[1], f[1, f[1, 2]], f[1, f[1, 2, 3]]} //. f[1, x__Integer] :> 1Replace occurrences of f with 1 followed by zero or more integers:
{f[1], f[1, f[1, 2]], f[1, f[1, 2, 3]]} //. f[1, x___Integer] :> 1ReplaceRepeated works inside held expressions:
Hold[{f[f[x]], f[x], g[f[x]], f[g[f[x]]]} ] //. f[x_] -> xDo not evaluate the right-hand side of the rule before doing the replacement:
Hold[{f[f[x]], f[x], g[f[x]], f[g[f[x]]]} ] //. f[x_] :> x + xEvaluate the right-hand side before replacement:
Hold[{f[f[x]], f[x], g[f[x]], f[g[f[x]]]} ] //. f[x_] -> x + xApplications (1)
Properties & Relations (5)
An empty list is considered to have no matching rules:
x //. {}When a list of lists is used for replacement, the result is a list of the same length:
x //. {{x -> 1}, {y -> 2}}Evaluation is not forced when replacing into a held expression:
{g[g[1]], Hold[g[g[1]]]} //. g[n_] -> n + 1ReplaceRepeated is effectively a combination of ReplaceAll and FixedPoint:
f[f[f[f[2]]]] //. f[2] -> 2FixedPoint[# /. f[2] -> 2&, f[f[f[f[2]]]]]ReplaceRepeated scans multiple times from the top of an expression:
rules = {f[x_] -> f[x, 5], f[x__] -> {x, 1}};{f[a], f[f[a, b]]} //. rulesReplaceAll replaces the largest subexpressions it can and then stops:
{f[a], f[f[a, b]]} /. rules%% == (% /. rules /. rules)Replace with level spec All will attempt to replace every subexpression exactly once:
Replace[{f[a], f[f[a, b]]}, rules, All]See Also
Tech Notes
Related Guides
Related Links
History
Introduced in 1988 (1.0) | Updated in 2018 (11.3)
Text
Wolfram Research (1988), ReplaceRepeated, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceRepeated.html (updated 2018).
CMS
Wolfram Language. 1988. "ReplaceRepeated." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2018. https://reference.wolfram.com/language/ref/ReplaceRepeated.html.
APA
Wolfram Language. (1988). ReplaceRepeated. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplaceRepeated.html
BibTeX
@misc{reference.wolfram_2026_replacerepeated, author="Wolfram Research", title="{ReplaceRepeated}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/ReplaceRepeated.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_replacerepeated, organization={Wolfram Research}, title={ReplaceRepeated}, year={2018}, url={https://reference.wolfram.com/language/ref/ReplaceRepeated.html}, note=[Accessed: 13-June-2026]}