在具有多个单词的表中搜索 [英] search in a table with multiple words

查看:109
本文介绍了在具有多个单词的表中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jquery中搜索多个单词时有一个代码.但我在搜索男性"和女性"这个词时遇到了问题.当我输入男性一词时,显示结果是女性和男性,因为女性一词包含"Fe" +"male"字符,我将如何解决此问题. 当我键入男性时,它应该只显示男性.而且只有当我输入"Male Female"或"Female"时,才会显示女性字词

i have a code in searching multiple words in jquery. but i have a problem on searching the word male and female. when i inputted the word male, the display result is female and male, because the word female contains "Fe" + "male" characters , how will i fix this problem. when i typed the male , it should only display the male. and the female word will be only displayed when i typed "Male Female" or "Female"

if (!RegExp.escape) {
RegExp.escape = function (s) {
    return s.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
};
}
jQuery(function ($) {
///search this table
$(' #search ').click(function () {
    var searchthis = new RegExp($(' #emp_search ').val().replace(/ /g,"|"), 'i');
    $("table").find("tr").slice(1).each(function (index) {
      //  var text = $(this).find("td").text().toLowerCase().trim();
        var text = $.trim($(this).text());
        $(this).toggle(searchthis.test(text));
    });
});
});

这是我的演示 http://jsfiddle.net/wind_chime18/ANLgD/10/ >

here is my demo http://jsfiddle.net/wind_chime18/ANLgD/10/

推荐答案

您可以使用\b检测单词边界:

You can use \b to detect word boundaries:

var s = "There are two sexes: female and male. males are blue, females are red";
s = s.replace(/\bmale\b/g, "smurf");
console.log(s);
// There are two sexes: female and smurf. males are blue, females are red"
// both female, males and females don't match \bmale\b

没有\b,您将得到:

There are two sexes: fesmurf and smurf. smurfs are blue, fesmurfs are red

如果您更改此行:

var searchthis = new RegExp($(' #emp_search ').val().replace(/ /g,"|"), 'i');

var searchthis = new RegExp("\\b" + $(' #emp_search ').val().replace(/ /g,"|") + "\\b", 'i');

有效,但这意味着搜索Jo也不会给您John.

it works but that means that searching for Jo will not give you John either.

这篇关于在具有多个单词的表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆