SearchBuilder plugin to match multiple values using RegEx
SearchBuilder plugin to match multiple values using RegEx
I've been playing around with SearchBuilder and came across a way to search for multiple values in a column using a REGEX search (that part isn't necessary, but it is what I wanted). It is based entirely on the example given in the SearchBuilder examples with a simple alteration.
Essentially, if you take that example, and just replace the search
portion with:
search: function (value, comparison) {
values = comparison[0].split(" ");
const regexes = values.map(pattern => new RegExp(pattern));
return regexes.some(regex => regex.test(value));
}
you will now have a search condition where you can type in values separated by a space and it will find any row that matches any one of those values.
For example, if I had a column that had every number from 0 - 400 and I wanted to pull back just those between 200 and 299 that ended in an 8 or 9, I would type 2.8 2.9
into the search field. The table would be filtered to just show those between 200 and 299 that ended in 8 or 9 (208, 209, 218, 219, 228, 229, etc)
Instead of using a REGEX expression (2.8) you can just use the values you are searching for separated by spaces. So if I take that same column with the list of numbers, I could just type 150 169 322 57 36
into the search field and it will pull back rows matching those numbers.
This is an easy alternative to having multiple OR statements. I've used it on data up to 18,000 records and it is plenty fast.
Long way
Easy way
Sorry if this is a no brainer and has already been shared.
Replies
That's superb - thanks very much for sharing that with us! I like it a lot
Allan