If this is something you find yourself needing to do fairly often (and, for some reason, don't wish to have to use Script Editor), then one way to do this is to create a systemwide Service (or Quick Action) in Automator:
It will be set to receive text in any application and ensure that the Output replaces selected text.
Once saved, you can select the text ...
You can't execute AppleScript directly from TextEdit. Apple provides an AppleScript editor though in /Applications/Utilities/Script\ Editor.app which makes it easy to create and run AppleScript code.
Just navigate to /Applications/Utilities in Finder, run Script Editor and copy/paste your code into it.