expr/.rules or ReplaceAll[expr,rules]
applies a rule or list of rules in an attempt to transform each subpart of an expression expr.
ReplaceAll[rules]
represents an operator form of ReplaceAll that can be applied to an expression.
ReplaceAll 
expr/.rules or ReplaceAll[expr,rules]
applies a rule or list of rules in an attempt to transform each subpart of an expression expr.
ReplaceAll[rules]
represents an operator form of ReplaceAll that can be applied to an expression.
Details
- ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part or on any of its subparts.
- ReplaceAll applies a particular rule only once to an expression.
- expr/.rules returns expr if none of the rules apply.
- ReplaceAll[rules][expr] is equivalent to ReplaceAll[expr,rules].
Examples
open all close allBasic Examples (5)
Replace a variable with a value:
{x, x ^ 2, y, z} /. x -> 1Replace a variable with a list:
{x, x ^ 2, y, z} /. x -> {a, b}Sin[x] /. Sin -> Cos{a, b, c} /. List -> fUse a pattern to bind a variable to a matching part:
1 + x ^ 2 + x ^ 4 /. x ^ p_ :> f[p]Apply the first matching rule:
x /. {x -> 1, x -> 3, x -> 7}x /. {{x -> 1}, {x -> 3}, {x -> 7}}Use ReplaceAll in operator form:
ReplaceAll[x -> a][{x, x ^ 2, y, z}]Scope (14)
If no rule matches, the original expression is returned:
x /. {y -> 2, z -> 3}Match expressions with one argument:
{f[2], f[x, y], h[], f[]} /. f[x_] -> "OK"Match expressions with one or more arguments:
{f[2], f[x, y], h[], f[]} /. f[x__] -> "OK"Match expressions with zero or more arguments:
{f[2], f[x, y], h[], f[]} /. f[x___] -> "OK"Replace expressions with a particular head:
{1, 3, 2, x, 6, Pi} /. _Integer :> "int"Use PatternTest to replace parts that yield True when a test function is applied:
{1, 3, 2, x, 6, Pi} /. _ ? PrimeQ -> "prime"Use Condition to restrict to parts that satisfy a Boolean expression:
{1, 3, 2, x, 6, Pi} /. t_ /; Mod[t, 3] == 0 -> "3k"{{a, 1}, {b, 2}} /. {x_, y_} :> Subscript[x, y]Replace each inner pair by restricting the pattern:
{{a, 1}, {b, 2}} /. {x_Symbol, y_} :> Subscript[x, y]ReplaceAll also works with RuleDelayed:
{x, x, x} /. x :> RandomReal[]Structurally insert into a held expression:
Hold[x + x] /. x -> 7Do not evaluate the right-hand side of the rule before doing the replacement:
Hold[x + x] /. x :> 2 ^ 2Evaluate the right-hand side before replacement:
Hold[x + x] /. x -> 2 ^ 2Dispatch can be used in place of a list of rules:
{1, 2} /. Dispatch[{1 -> a, 3 -> b}]Association can be used in place of a list of rules:
{a, b} /. Association[{a -> 1, c -> 2}]Use Association as a pattern for replacement:
<|a -> 1|> /. <|a -> x_|> :> xUse KeyValuePattern to replace in a list of rules:
{a -> 1, b -> 2} /. KeyValuePattern[{a -> x_}] -> xExtract the key from an Association that matches a rule:
ReplaceAll[<|a -> 1, b -> 2, c -> 3|>, KeyValuePattern[{x_ -> 1}] :> x]Find a key in a list of rules satisfying a criterion:
{a -> 1, b -> 2} /. KeyValuePattern[{x_ -> _ ? EvenQ}] -> xProperties & Relations (7)
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}}Only the first rule that matches is applied to each part:
{a, b, c} /. {a -> b, b -> d}Use iterated calls to ReplaceAll to apply each rule to all parts:
{a, b, c} /. a -> b /. b -> dEvaluation is not forced when replacing into a held expression:
{g[1], Hold[g[1]]} /. g[n_] -> n + 1ReplaceAll replaces any matching part:
f[1, 2, z] /. _Integer :> "int"Replace replaces a whole expression by default:
Replace[f[1, 2, z], _Integer :> "int"]It also allows specific levels to be replaced:
Replace[f[1, 2, z], _Integer :> "int", {1}]ReplaceAll replaces the largest subexpressions it can and then stops:
rules = {f[x_] -> f[x, 5], f[x__] -> {x, 1}};{f[a], f[f[a, b]]} /. rulesReplaceRepeated repeatedly applies ReplaceAll until the expression stops changing:
{f[a], f[f[a, b]]} //. rules% === ({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]ReplaceAll replaces parts of expressions that match a pattern:
{5, 4, 3, 2, 1} /. _ ? PrimeQ -> "p"ReplacePart replaces parts of expressions whose positions match a pattern:
ReplacePart[{5, 4, 3, 2, 1}, _ ? PrimeQ -> "p"]Possible Issues (4)
Applying a long list of rules can be slow:
rules = Table[x[i] -> RandomInteger[{1, i}], {i, 10000}];Timing[Table[x[i] /. rules, {i, 10000}];]Using Dispatch can be significantly faster:
disp = Dispatch[rules];Timing[Table[x[i] /. disp, {i, 10000}];]Associations can be used to specify replacement rules, but the keys are treated as Verbatim values:
assoc = <|1 -> True, _ -> False|>;{1, 2, _} /. assoclist = Normal[assoc]{1, 2, _} /. listThe list of rules equivalent to the association is the following:
{1, 2, _} /. {1 -> True, Verbatim[_] -> False}Periods bind to numbers more strongly than to slash, so the following is a division operation:
{0, 1} / .0 -> "zero"Insert a space between /. and numbers to avoid that issue:
{0, 1} /. 0 -> "zero"Most patterns are only compared against the values of an association:
<|a -> f[b -> c]|> /. (k_ -> v_) :> g[k, v]For compound patterns matching an Association, inner patterns match both keys and values:
<|a -> f[b -> c]|> /. _[k_ -> v_] :> g[k, v]By excluding heads matching Association, only comparisons against values are done:
<|a -> f[b -> c]|> /. Except[Association][k_ -> v_] :> g[k, v]See Also
Replace Rule ReplaceAt Set MapAll ReplaceRepeated TransformationFunctions ReplaceList
Function Repository: ReplaceAllUnheld ReplaceAllOutside RecursiveRewrite
Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 2014 (10.0) ▪ 2015 (10.3)
Text
Wolfram Research (1988), ReplaceAll, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAll.html (updated 2015).
CMS
Wolfram Language. 1988. "ReplaceAll." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/ReplaceAll.html.
APA
Wolfram Language. (1988). ReplaceAll. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplaceAll.html
BibTeX
@misc{reference.wolfram_2026_replaceall, author="Wolfram Research", title="{ReplaceAll}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ReplaceAll.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_replaceall, organization={Wolfram Research}, title={ReplaceAll}, year={2015}, url={https://reference.wolfram.com/language/ref/ReplaceAll.html}, note=[Accessed: 13-June-2026]}