如何使用空格来创建正则表达式来过滤datatable [英] How to create regex with whitespace for filtering datatable

查看:240
本文介绍了如何使用空格来创建正则表达式来过滤datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有几个预定义的过滤器选项来过滤我的数据表。当单击一个时,它将被添加到特定列的过滤器中。但是我有一个很大的问题与空白...例如我有这些选项:



老和好,便宜和好,昂贵的



所有点击的选项都存储在数组中。对于过滤我做

  var term = array.join('|'); //  - > term =old& good | cheap& nice | expensive
dataTable.fnFilter(term,column,true,true);

如果没有空格的字符串,&或只有一个。
a solocheap& nice正在工作,但廉价&好|昂贵的不...也没有昂贵的|有史以来的工作,当没有空白。



如何让过滤器工作?



--------编辑--------- -



一些进一步的解释:



我的代码从(用户定义)csv导入数据,并为第一列有一些按钮,一个用于此列中每个唯一的条目。用户可以点击按钮过滤表格,只显示点击的条目。该按钮获取条目作为值,例如在此示例中。

 < input type =buttonvalue =cheap and amp ; niceonclick =myFilterFunction(this)/> 

要知道按钮已被点击,它们的值被存储在一个正常的数组中。我的回调函数的点击查看这个数组,将名称加到一个正则表达式,并将这个字符串给fnFilter(),就像上面发贴。



只要没有包含空格的条目,这样就可以正常工作。但是我不能删除空格,因为过滤器找不到真正有空白的条目。

解决方案

你应该引用特殊字符。



如果这是常量字符串,只需使用斜杠'old /&好'



如果这是用户输入,你应该使用quote功能。 JS中默认没有这样的功能,但您可以使用 http://phpjs.org/functions/preg_quote/

  function preg_quote(str,delimiter){
return(str +'').replace新的RegExp('[\& .\\\\ + *?\\ [\\ ^ \\] $(){} =!<> |:\\ \\\'+(delimiter ||'')+' - ]','g'),'\\ $&');
}

所以如果你有这样的用户输入:

  var input = [old& good,cheap and nice,expensive]; 
var regexp_string = input.map(function(val){return preg_quote(val,/)})。join(|);


I've a few predefined filter options on my page to filter my datatable. When clicking one, it gets added to the filter of the specific column. But I've a big problem with whitespaces... for example I've those options:

"old & good", "cheap & nice", "expensive"

All clicked options are stored in an array. For filtering I do

var term = array.join('|'); //--> term = "old & good|cheap & nice|expensive"
dataTable.fnFilter(term, column, true, true);

It works fine if there are no strings with whitespaces and "&" or only one. a solo "cheap & nice" is working, but "cheap & nice|expensive" doesn't... also "expensive|what|ever" works when there are no whitespaces.

How can I get the filter work?

--------edit----------

some further explanation:

My code imports data from (user-defined) csv and generates for the first column some buttons, one for every unique entry in this column. The user can click on the buttons to filter the table, only the clicked entrys should be displayed. The button gets the entry as value, like in this example.

<input type="button" value="cheap & nice" onclick="myFilterFunction(this)"/>

To know wich buttons have been clicked, their value gets stored in a normal array. My callback function for the click looks into this array, joins the names to one regexp and gives this string to the fnFilter(), like posted above.

This works fine as long as there is no entry with whitespaces in it. But i can't remove the whitespaces, because then the filter can't find the entry wich really has whitespaces in it.

解决方案

You should quote special characters.

If this is constant string just use slash 'old /& good'

If this is user input you should use quote function. There is no such function by default in JS, but you can use http://phpjs.org/functions/preg_quote/

    function preg_quote (str, delimiter) {
      return (str + '').replace(new RegExp('[\&.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
    }

So if you have user input like:

    var input = ["old & good","cheap & nice","expensive"];
    var regexp_string = input.map(function(val){ return preg_quote(val,"/") }).join("|");

这篇关于如何使用空格来创建正则表达式来过滤datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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