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

# replacing(subrange:maxReplacements:content:with:)

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

## Declaration

```swift
func replacing<Output, Replacement>(subrange: Range<Self.Index>, maxReplacements: Int = .max, @RegexComponentBuilder content: () -> some RegexComponent, with replacement: (Regex<Output>.Match) throws -> Replacement) rethrows -> Self where Replacement : Collection, Replacement.Element == Character
```

## Parameters

- `subrange`: The range in the collection in which to search 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.
- `replacement`: A closure that receives the full match information, including captures, and returns a replacement collection.

## Return Value

Return Value A new collection in which all matches for regex in subrange are replaced by the result of calling replacement, where regex is the result of calling content.
