<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/RangeReplaceableCollection/replacing(maxReplacements:content:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sm12RegexBuilderSKRzSs11SubSequenceSlRtzrlE9replacing15maxReplacements7content4withxSi_qd_1_yXEqd_0_17_StringProcessing0A0V5MatchVyqd___GKXEtK0A6OutputQyd_1_Rsd__SlRd_0_AH0A9ComponentRd_1_SJ7ElementRtd_0_r1_lF"
  },
  "title" : "replacing(maxReplacements:content:with:)"
}
-->

# replacing(maxReplacements:content:with:)

Returns a new collection in which all matches for the regex
are replaced, using the given closures to create the replacement
and the regex.

```
func replacing<Output, Replacement>(maxReplacements: Int = .max, @RegexComponentBuilder content: () -> some RegexComponent, with replacement: (Regex<Output>.Match) throws -> Replacement) rethrows -> Self where Replacement : Collection, Replacement.Element == Character
```

## Parameters

`maxReplacements`

A number specifying how many occurrences of
the regex to replace, using `content` to create the regex.

`content`

A closure that returns the collection to search for
and replace.

`replacement`

A closure that receives the full match information,
including captures, and returns a replacement collection.

## Return Value

A new collection in which all matches for regex in `subrange`
are replaced by the result of calling `replacement`, where regex is
the result of calling `content`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)