<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/RangeReplaceableCollection/replacing(_:with:subrange:maxReplacements:)-5mdsz",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sm17_StringProcessingSs11SubSequenceRtzrlE9replacing_4with8subrange15maxReplacementsxqd_0__qd__SnySS5IndexVGSitSlRd__AA14RegexComponentRd_0_SJ7ElementRtd__r0_lF"
  },
  "title" : "replacing(_:with:subrange:maxReplacements:)"
}
-->

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

Returns a new collection in which all occurrences of a sequence matching
the given regex are replaced by another collection.

```
func replacing<Replacement>(_ regex: some RegexComponent, with replacement: Replacement, subrange: Range<Self.Index>, maxReplacements: Int = .max) -> Self where Replacement : Collection, Replacement.Element == Character
```

## Parameters

`regex`

A regex describing the sequence to replace.

`replacement`

The new elements to add to the collection.

`subrange`

The range in the collection in which to search for `regex`.

`maxReplacements`

A number specifying how many occurrences of the
sequence matching `regex` to replace. Default is `Int.max`.

## Return Value

A new collection in which all occurrences of subsequence
matching `regex` in `subrange` are replaced by `replacement`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)