var is=1
var string='Hello <span id="esui0" class="bold">a<span id="esui0" class="underline">mig</span>o</span>)<span class="inactive" id="anchor1"></span>some text<span class="some-class">and some text</span><span class="inactive" id="anchor0"></span>';
var enteredREN=new RegExp('(^.*anchor'+(is-1)+'[^>]*></span>)(.*)(<span.*anchor'+is+'.*$)','ig');
var enteredRENN=new RegExp('(^.*anchor'+is+'[^>]*></span>)(.*)(<span.*anchor'+(is-1)+'.*$)','ig');
if ( enteredREN.test( string ) ) {
var entered= string.replace(enteredREN,'$2');
} else {
var entered=string.replace(enteredRENN,'$2');
}
alert( entered );