我得到了這個搜索表與多個單詞和顯示多個結果。但是當我搜索「男性」時,我在搜索確切詞「男性」和「女性」時遇到問題,女性也顯示,因爲女性有「男性」一詞。我將如何找到確切的單詞?如何搜索表中的確切單詞jquery
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');
alert(searchthis);
$("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/11/
不知道是否爲J avascript正則表達式,但嘗試字邊界\ B運營商「\ Bmale \ B」可能會做詭計 – vogomatix
@vogomatix試試我的jsfiddle先生。 – Vincent
遺憾沒時間看,但是http://www.javascriptkit.com/jsref/regexp.shtml建議實現字邊界。 – vogomatix