---
title: "replaceMatches(in:options:range:withTemplate:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsregularexpression/replacematches(in:options:range:withtemplate:)"
---

# replaceMatches(in:options:range:withTemplate:)

Replaces regular expression matches within the mutable string using the template string.

## Declaration

```swift
func replaceMatches(in string: NSMutableString, options: NSRegularExpression.MatchingOptions = [], range: NSRange, withTemplate templ: String) -> Int
```

## Parameters

- `string`: The mutable string to search and replace values within.
- `options`: The matching options to use. See doc://com.apple.foundation/documentation/Foundation/NSRegularExpression/MatchingOptions for possible values.
- `range`: The range of the string to search.
- `templ`: The substitution template used when replacing matching instances.

## Return Value

Return Value The number of matches.

## Discussion

Discussion See Flag Options for the format of templ.

## See Also

### Replacing Strings Using Regular Expressions

- [stringByReplacingMatches(in:options:range:withTemplate:)](foundation/nsregularexpression/stringbyreplacingmatches(in:options:range:withtemplate:).md)
