[How to] Make filter "text contains" not only look for whole words

It’s been a couple times where I needed to filter a list of things based on a user keyword input. The straight way to do it is to use a constraint that displays only the items where a field contains the specified keyword, like so:

The problem is that this rule while look for whole words only… So an item with a value “hello world” won’t show up in the list if the user searches for “worl” or “hel”. Annoying… This was supposed to be fixed back in 2016 but still isn’t. I supposed they have bigger rabbits to chase.

Anyways… I came up with a simple workaround that does the job well enough for me: including a very very simple regex extraction in the constraint. Here’s how it looks:


And the regex pattern (which really is just the content of the text input in lowercase):
37

I can already hear some bubblers screaming “DON’T EVER USE ADVANCED FILTERS!!!” I know advanced filters and filtering after the search should be used only when needed, but hey, this is a workaround… In my real world app, I load my list of things on page load and refer to it afterwards to apply the filtering.

The rule explained:
In the above screenshot the rule only shows items for which the condition evaluates to true.

  • For each item of the list, it starts by converting it to lowercase to prevent missing out on items that contain capital letters.
  • Then it uses Regex to extract a substring from the content where the substring is the content of the text input (also lowercased). This is where we work around the limited “contains” built-in feature.
  • The output of the Regex extraction is a list of strings that match the Regex pattern. So if the ouput list is empty, no match were found; the rule evaluates to false so the item won’t display in the list.
  • Finally there is “or Search input's value is empty”. We need this exception to show all items when the search text input is empty.

2019-11-25%2015_24_23

4 Likes

Do you always need it to be exact? There are some fuzzy logic auto complete search plugins that can do the job and also be ok with some level of typos.

Just a thought. Great work around though!

Do you always need it to be exact?

About what?

Yeah these plugins are much more complex and powerful than this solution. In the context of this post, these plugins would be overkill.