jquery REGEX匹配精确的术语



我使用的是带有自定义分面导航的jQuery和DataTable。我创建了每个复选框的值的管道分隔列表,以填充dataTable的fnFilter。取消选中一个框,它会重新构建没有该值的列表。它运行良好,除非有"工程"one_answers"机械工程"这样的值。如果勾选了"工程"值,但没有勾选"机械工程"值时,则不限制勾选"力学工程"(因为"工程"的值符合两者的标准)。我正在尝试使用REGEX修改字符串,以将术语限制在字符串中。

第一排工程

第二排机械工程

第三排工程、机械工程、电气工程

因此,通过取消勾选"工程",第二行和第三行将保留,但第一行将过滤掉。

我已经对此进行了修改,以包含更完整的代码。我不知道如何在这里设置演示,但我很乐意这样做。

//dataTable configuration
$('#directoryTable').dataTable({
"sDom": 'lprtpi',
"iDisplayLength": 25,
"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
"aaSorting": [[0,'asc'],[1,'asc']],
"aoColumns": [
{"bSortable": true},
{"bSortable": true},
{"bSortable": false},
{"bSortable": false,"sSortDataType": "dom-checkbox" }
]
})

//custom filtering on industry clusters     
var oTable = $('#directoryTable').dataTable();
$('input[class="cluster"]').click(function(){
//get list of values of checked boxes in cluster class
var checkedList = $('input[class="cluster"]:checked').map(function() {return this.value;}).get().join('|');
//var checkedList = $('input[class="cluster"]:checked').map(function() {return "^"+this.value+",|^"+ this.value+"$|,"+ this.value+"$|,"+ this.value+",";}).get().join('|');
window.console&&console.log(checkedList);
//limit results to selected values
if ($('input[class="cluster"]').is(":checked")) {
//uses a REGEX filter with a | delimited (OR) filter, only on the third column
oTable.fnFilter(checkedList, 2, true,false);
//window.console&&console.log('clicked ' + $(this).val());
//clear the filter when unchecked
} else {
oTable.fnFilter('',2);
oTable.fnFilter('');
}
});

我是jQuery的新手,我真的很感激任何想法。

如果这是不正确的,我很抱歉,但如果没有您的全部代码,我无法确定这里的内容是否有效。我希望它能有所帮助。

var reg = /^Engineering$/gi;
var a = ['Mechanical Engineering','engineering','Silver Mining','Engineering','Silver and Gold','Electrical Engineering'];
var items = jQuery(a).filter(function(i,v){
return reg.test(v);
}).get().join('|');
// items returns engineering|Engineering
// I believe your reg, would just be value of the element that is clicked/unclicked.

所以,如果你想动态地传递这个,可以做一些类似的事情:

//some function to capture the arg/checkbox value
var val = arg,
reg = new RegExp('^' + val + '$', "gi");
reg.test(v); // v being the string passed in for your reiteration.

最新更新