使用jquery从文本区域搜索单词

时间:2015-10-03 08:13:10

标签: javascript jquery html ajax

我正在尝试搜索textarea中的一些单词。这些单词取自不同的文本框。它工作正常但是当我搜索具有某些常用名称的单词(nokia lumia和nokia lumia 310)时,它并没有给我完美的结果。我想搜索完整的句子,然后锁定该句子,以便它不会被用于其他搜索。

http://i.stack.imgur.com/En6kn.png

在这张图片中,我们有多个要搜索的单词,但只有少数单词,而不是整个句子。

HTML CODE:

 <script>
 $(document).ready(function() {
 doit();
 function   doit()
{

$('#txtara').highlightTextarea('destroy');
var $wrds = [];

var txt1=$('#txt1').val();

if(txt1!="")
 {

 $wrds.push({
color: '#FFFF00',

 words: $('#txt1').val()

});
}
 if($('#txt2').val()!="")
 {
 $wrds.push({color: '#ADF0FF', words: $('#txt2').val()});

 }

if($('#txt3').val()!="")
{
$wrds.push({color: '#ADA0F0', words: $('#txt3').val()});
}
  if($('#txt4').val()!="")
{
$wrds.push({color: '#9fc1f0', words: $('#txt4').val()});
}
 if($('#txt5').val()!="")
{
$wrds.push({color: '#3eb5ac', words: $('#txt5').val()});
}
 if($('#txt6').val()!="")
{
$wrds.push({color: '#e7c4a9', words: $('#txt6').val()});
}

 if($('#txt7').val()!="")
{
$wrds.push({color: '#cee2ad', words: $('#txt7').val()});
}

 if($('#txt8').val()!="")
{
 $wrds.push({color: '#8a6396', words: $('#txt8').val()});
  }

   if($('#txt9').val()!="")
  {
  $wrds.push({color: '#380ced', words: $('#txt9').val()});
  }

 if($('#txt10').val()!="")
 {
 $wrds.push({color: '#4ffb64', words: $('#txt10').val()});
 }

if($('#txt11').val()!="")
  {
 $wrds.push({color: '#9cf3c4', words: $('#txt11').val()});
 }

  if($('#txt12').val()!="")
 {
 $wrds.push({color: '#e64f28', words: $('#txt12').val()});
  }


  $('#txtara').highlightTextarea({
      words: $wrds,
      debug: false,
         caseSensitive: false
  });

 }// END OF DOIT FUNC








 $(document.body).on('keyup', ".clsdoit", function(){
 doit();
 });






    $(".text").on('keyup',function(){

search();
search1();
search2();
search3();
search4();
search5();
search6();
search7();
search8();
search9();
search10();
search11();
});
function setCharAt(str,index,chr) {
        if(index > str.length-1) return str;
        return str.substr(0,index) + chr + str.substr(index+1);
    }

function search(){  
var a = $('#txt1').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab1').html(totalcount);
}   






function search1(){ 
var a = $('#txt2').val().toLowerCase();
var totalcount=0;
 var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab2').html(totalcount);
}   

function search1(){ 
var a = $('#txt2').val().toLowerCase();
var totalcount=0;
 var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab2').html(totalcount);
}   
function search2(){ 
var a = $('#txt3').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab3').html(totalcount);
}   
function search3(){ 
var a = $('#txt4').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab4').html(totalcount);
}   
function search4(){ 
var a = $('#txt5').val().toLowerCase();
var totalcount=0;
 var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab5').html(totalcount);
}   
function search5(){ 
var a = $('#txt6').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab6').html(totalcount);
}   
function search6(){ 
var a = $('#txt7').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab7').html(totalcount);
}   
function search7(){ 
var a = $('#txt8').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab8').html(totalcount);
}   
function search8(){ 
 var a = $('#txt9').val().toLowerCase();
 var totalcount=0;
 var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab9').html(totalcount);
}   
function search9(){ 
var a = $('#txt10').val().toLowerCase();
var totalcount=0;
 var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab10').html(totalcount);
}   
function search10(){    
var a = $('#txt11').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab11').html(totalcount);
}   
function search11(){    
var a = $('#txt12').val().toLowerCase();
var totalcount=0;
var value = $('.text').val().toLowerCase();
 var regex = /\s+/gi;
 var wordCount = value.trim().replace(regex, ' ').split(' ').length;
 var values = value.trim().replace(regex, ' ');



 for(var i = 0; i < value.length; i++)
 {
    var index = value.toLowerCase().indexOf(a);
    if(index > 0)
    {
        totalcount++;
        value = setCharAt(value,index, '' );
    }
 }

$('#lab12').html(totalcount);
}







});

jquery代码

{"status": "failure", "code": 7, "host": "coreapp-ngapi-prod-cc8abc1f", "generated_at": "Sat, 03 Oct 2015 08:05:09 +0000", "message": "You are not permitted to access that resource.", "data": "The provided redirect_uri does not match any of the registered redirect URIs."}

1 个答案:

答案 0 :(得分:0)

我没有使用jquery highlighter插件,但为了迭代每个句子的正则表达式,你可以用while循环做这样的事情。你必须关闭use strict。

struct