<!--
{
  "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/replace(with:maxReplacements:content:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sm12RegexBuilderSKRzSs11SubSequenceSlRtzrlE7replace4with15maxReplacements7contentyqd___Siqd_0_yXEtSlRd__17_StringProcessing0A9ComponentRd_0_SJ7ElementRtd__r0_lF"
  },
  "title" : "replace(with:maxReplacements:content:)"
}
-->

# replace(with:maxReplacements:content:)

Replaces all matches for the regex in this collection, using the given
closure to create the regex.

```
mutating func replace<Replacement>(with replacement: Replacement, maxReplacements: Int = .max, @RegexComponentBuilder content: () -> some RegexComponent) where Replacement : Collection, Replacement.Element == Character
```

## Parameters

`replacement`

The new elements to add to the collection in place of
each match for the regex, using `content` to create the regex.

`maxReplacements`

A number specifying how many occurrences of
the regex to replace.

`content`

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

---

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)