tzn tzn - 2 months ago 17
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] http://regexr.com/3e9gc 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:
http://codepen.io/anon/pen/pENYZE

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

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():

    Plutonium
    Pu
    94
    (244)

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

CodePen

Comments