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

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

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

## Declaration

```swift
func replacing<Replacement>(with replacement: Replacement, subrange: Range<Self.Index>, maxReplacements: Int = .max, @RegexComponentBuilder content: () -> some RegexComponent) -> Self 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.
- `subrange`: The range in the collection in which to search for 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.

## Return Value

Return Value A new collection in which all matches for regex in subrange are replaced by replacement, using content to create the regex.
