如何用TinyMCE替换元素

I'm using TinyMCE and i'm creating a new plugin.

I need, in this plugin, to replace something i have selected in the text by an other thing.

With this : var inst = tinyMCE.activeEditor; i can have a instance of my editor.

How can i retrieve my selection ? How can i replace my selection by, for example <span>MYSELECTION</span> ?

Thanks !

Try this:

<a href="#" onclick="tinyMCE.execCommand('mceReplaceContent',false,'<span>{$selection}</span>');return false;">[Replace selection]</a>

EDIT: About retrieving selection see this question.