JavaScript localiza a posição de ocorrência do texto selecionado em um div

Eu tenho uma string com uma palavra cinco vezes. Se eu selecionei olá, ela deve retornar 4

 <div id="content">hello hai hello hello hello</div>

recebendo script de texto selecionado

<script>
 if(window.getSelection){
   t = window.getSelection();
 }else if(document.getSelection){
   t = document.getSelection();
 }else if(document.selection){
   t =document.selection.createRange().text;
 }
 </script>

Se estou selecionandohai deve retornar1.

Se estou selecionandohello hai deve retornar1. por favor ajude.

questionAnswers(2)

yourAnswerToTheQuestion