---
title: "replace(maxReplacements:content:with:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/rangereplaceablecollection/replace(maxreplacements:content:with:)"
---

# replace(maxReplacements:content:with:)

Replaces all matches for the regex in this collection, using the given closures to create the replacement and the regex.

## Declaration

```swift
mutating func replace<Output, Replacement>(maxReplacements: Int = .max, @RegexComponentBuilder content: () -> some RegexComponent, with replacement: (Regex<Output>.Match) throws -> Replacement) rethrows 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.
