<!--
{
  "availability" : [
    "iOS: 17.4.0 -",
    "iPadOS: 17.4.0 -",
    "tvOS: 17.4.0 -",
    "visionOS: 1.1.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "BrowserEngineKit",
  "identifier" : "/documentation/BrowserEngineKit/BETextInput/replaceSelectedText(_:withText:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "BrowserEngineKit"
    ],
    "preciseIdentifier" : "c:objc(pl)BETextInput(im)replaceSelectedText:withText:"
  },
  "title" : "replaceSelectedText(_:withText:)"
}
-->

# replaceSelectedText(_:withText:)

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

```
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.

## 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.

---

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)