---
title: "replace(with:maxReplacements:content:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/rangereplaceablecollection/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.

## Declaration

```swift
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.
