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

# replace(_:maxReplacements:with:)

Replaces all occurrences of the sequence matching the given regex with a given collection.

## Declaration

```swift
mutating func replace<Output, Replacement>(_ regex: some RegexComponent, maxReplacements: Int = .max, with replacement: (Regex<Output>.Match) throws -> Replacement) rethrows where Replacement : Collection, Replacement.Element == Character
```

## Parameters

- `regex`: A regex describing the sequence to replace.
- `maxReplacements`: A number specifying how many occurrences of the sequence matching regex to replace. Default is Int.max.
- `replacement`: A closure that receives the full match information, including captures, and returns a replacement collection.
