---
title: "replaceSelectedText(_:withText:)"
framework: browserenginekit
role: symbol
role_heading: Instance Method
path: "browserenginekit/betextinput/replaceselectedtext(_:withtext:)"
---

# replaceSelectedText(_:withText:)

Replaces text with new text, either within the current selection or near the cursor.

## Declaration

```swift
func replaceSelectedText(_ text: String, withText replacementText: String)
```

## Parameters

- `text`: The text to find and replace.
- `replacementText`: The text to insert in place of the found text.

## Mentioned in

Integrating custom browser text views with UIKit

## Discussion

Discussion This method’s behavior depends on the current selection state: If text is selected, this method replaces occurrences of text within the selection with replacementText. If the cursor is positioned without a selection, this method searches for text immediately before the cursor and replaces it with replacementText. If the cursor is at the start of an editable element, this method searches for text immediately after the cursor instead.

## See Also

### Inserting and replacing text

- [insert(_:)](browserenginekit/betextinput/insert(_:)-5iryn.md)
- [insert(_:)](browserenginekit/betextinput/insert(_:)-6x7hd.md)
- [replaceDictatedText(_:withText:)](browserenginekit/betextinput/replacedictatedtext(_:withtext:).md)
- [add(_:)](browserenginekit/betextinput/add(_:).md)
