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 in TextEdit
(or, indeed, any application), and have it execute the AppleScript in situ, returning the result, which will appear below the selection.
AppleScript code:
on run {{input}}
local input
try
return {input, run script input}
on error E
return {input, E}
end try
end run