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

# replace(_:with:maxReplacements:)

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

## Declaration

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

## Parameters

- `regex`: A regex describing the sequence to replace.
- `replacement`: The new elements to add to the collection.
- `maxReplacements`: A number specifying how many occurrences of the sequence matching regex to replace. Default is Int.max.
