I have a simple filter function in my javascript, based on an input box.
function filter(selector, query) {
query = $.trim(query); //trim white space
query = query.replace(/ /gi, '|'); //add OR for regex
$(selector).each(function() {
($(this).text().search(new RegExp(query, "i")) < 0) ? (do something here)
So, if I have a table with a list of words, eg.
Alpha Centauri,
Beta Carbonate,
Charly Brown,
...
and I enter 'alpha cen' into my input box, the function searches for ('alpha' or 'cen') and returns the one record as desired.
However, if I replace the '|
' with a '&
' to search for ('alpha' and 'cen') I get no results. Even if I enter 'alpha centauri', I get no result at all.
Why?
Answer
While a |
in a regex allows alternation, an &
carries no special meaning. Your current code is trying to find a literal match for alpha&cen
, which clearly doesn't match any of your data.
No comments:
Post a Comment