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

# replacing(with:maxReplacements:content:)

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

```
func replacing<Replacement>(with replacement: Replacement, maxReplacements: Int = .max, @RegexComponentBuilder content: () -> some RegexComponent) -> Self 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 regex
to replace.

`content`

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

## Return Value

A new collection in which all matches for regex in `subrange`
are replaced by `replacement`, using `content` to create the regex.

---

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)