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

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

Returns a new string containing matching regular expressions replaced with the template string.

## Declaration

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

## Parameters

- `string`: The string to search for 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 A string with matching regular expressions replaced by the template string.

## Discussion

Discussion See Flag Options for the format of templ.

## See Also

### Replacing Strings Using Regular Expressions

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