tzn tzn - 4 months ago 35
jQuery Question

Isotope regex search filtering by word at the beginning of line

I would like to filter results in Isotope by matching only beginning of line strings. I know regexp should be something along the lines of [/^\b($string)/igm] but I can't seem to make it work.

Here is Isotope's own codepen example on which I have tried to apply this regexp:

qsRegex = new RegExp( "\\b("+$quicksearch.val()+")", 'gmi' );

... is the code line in question. I can't seem to properly include ^ as well.

In Search field start typing Ca - Calcium, Cadmium and edited Rhenium Cadmiumca will appear. Third one shouldn't be there (or any other combination).

Thank you for your help.


What's happening is it's using $this.text() to get all text within each element box, then running the regex on that. That text includes all sorts of spaces and such, so you have to be more flexible to get it to work correctly.

Example of $this.text():


Adding \s* after the line-start allows any number of whitespace characters to appear next, before the word itself.

  qsRegex = new RegExp( "^\\s*\\b("+$quicksearch.val()+")", 'gmi' );