tzn tzn - 1 year ago 93
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.

Answer Source

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' );


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download