微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – RegExp不区分大小写的多字高亮

我正在努力突出关键字搜索工作正常.我遇到的几个问题.

>不区分大小写对第一个单词起作用,但希望用原始单词替换,而不是小写搜索单词.

搜索趋势,它取代趋势与趋势,我知道为什么,但想弄清楚如何替换找到的单词,而不是搜索到的单词

>第二个词不匹配不区分大小写.

搜索趋势微观不匹配趋势Micro.

这是jsfiddlehttp://jsfiddle.net/hh2zvjft/1/

if ($(".ProjectSearch").val().length > 0) {
    var searchedText = $(".ProjectSearch").val();
    var wordList = searchedText.split(" ");
    $.each(wordList, function (i, word) {
        $(".ProjectTaskGrid:contains('" + word + "')").each(function (i, element) {
            var rgxp = new RegExp(word, "gi");
            var repl = '<span class="search-found">' + word + '</span>';
            element.innerHTML = element.innerHTML.replace(rgxp, repl);
        });
    });
}

您能否帮助确定问题并提供改进?谢谢!

一些引用用于获得代码

https://stackoverflow.com/a/120161/2727155

https://stackoverflow.com/a/10011639/2727155

解决方法:

突出显示多个单词(忽略HTML标记)

我就是这样做的:jsBin demo

var $input = $("#input");
var $text = $("#area");
var org = $text.html() || "";

$input.on("input", function(){
  
  var res,
      reg,
      words = [],
      val = $.trim(this.value.replace(/[^\w\s]/gi, '')); // remove Special Chars
  
  if(val.length > 0){
    words = val.split(" ");
    reg = new RegExp("(?![^<]+>)("+ words.join("|") +")", "ig");
    res = org.replace(reg, "<span class='highlight'>$1</span>");
  }
  
  $text.html(words.length > 0 ? res : org);

}).trigger("input");
.highlight {
  background: gold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input" type="text" value="tren pan com br" />

<div id="area">Renew Trend Worry-Free Business Security license that <a href="http://someweb.com">someweb.com</a> will expire in 60 days.<br>
  Activate BR like breakline trend and [ confirm <span>SOME <span>SPAN</span> IS HERE</span> upon electronic<br> delivery notification from Trend Micro</div>

非常简单和酷,因为代码不会混乱(匹配)HTML标记中的字符串.

突出显示和排序表行

在这里使用相同的原则是一个自动疼痛表行的例子(热点thead的标题行):

$("input[data-highlightsort]").each(function(){

  var dataTarget = $(this).data("highlightsort");
  var $tbody = $("table[data-highlightsort='"+dataTarget+"'] tbody");
  var org = $tbody.html() || "";

  $(this).on("input", function(){
    
    var res,
        reg,
        words,
        val = $.trim(this.value.replace(/[^\w\s]/gi, '')), // remove Special Chars
        hasVal=val.length>0;
    
    if(hasVal){
      words = val.split(" ");
      reg = new RegExp("(?![^<]+>)("+ words.join("|") +")", "ig");
      res = org.replace(reg, "<span class='highlight'>$1</span>");
    }

    // Highlight
    $tbody.html(hasVal ? res : org);

    // Show/hide
    if(hasVal) $tbody.find("tr").hide().has(".highlight").show();

  }).trigger("input");

});
.highlight{
  background: gold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="searchSort" type="text" data-highlightsort="table1" value="co">


<table data-highlightsort="table1">
  <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Nickname</th>
      <th>Profile</th>
      <th>Date</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Roko</td>
      <td>C. Buljan</td>
      <td>roXon</td>
      <td><a href="https://stackoverflow.com/users/383904/roko-c-buljan?tab=profile">roko-c-buljan</a></td>
      <td>2010</td>
    </tr>
    <tr>
      <td>2</td>
      <td>stack</td>
      <td>Overflow</td>
      <td>SO</td>
      <td><a href="https://stackoverflow.com">stackoverflow.com</a></td>
      <td>2008</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Community</td>
      <td></td>
      <td></td>
      <td><a href="https://stackoverflow.com/users/-1/community">community</a></td>
      <td>2008</td>
    </tr>
  </tbody>
</table>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐