The return you expect from the function within the .not()
must be a element, one selector or a array (documentation of .not()
). As it stands, the return is being a boolean true or false, which has no effect on the method.
Change the return
for a ternary that will return the element itself (if the condition is true
) or null (if it’s false
). That is, by returning the element itself, it will not suffer the .hide()
posterior.
return matcher.test($(this).find('.name, category').text()) ? $(this) : null;
Regarding the search, there are two ways to solve this:
1. Using .test()
:
Remove the flag g
(global), because the behavior of this flag with .test
and .exec
is different from .match
(document to get a sense of this).
$("#box").on('keyup', function(){
var matcher = new RegExp($(this).val(), 'i');
$('.connect-cat').show().not(function(){
return matcher.test($(this).find('.name, category').text()) ? $(this) : null;
}).hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder='Search' id='box' type='text' />
<div style='clear:both;'></div>
<div class='connect-cat'>
<span class='name'>tomatoes</span>
</div>
<div class='connect-cat'>
<span class='name'>tomato</span>
</div>
<div class='connect-cat'>
<span class='name'>tomatos</span>
</div>
<div class='connect-cat'>
<span class='name'>apple</span>
</div>
2. Using .match()
:
$("#box").on('keyup', function(){
var matcher = new RegExp($(this).val(), 'gi');
$('.connect-cat').show().not(function(){
return $(this).find('.name, category').text().match(matcher) ? $(this) : null;
}).hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder='Search' id='box' type='text' />
<div style='clear:both;'></div>
<div class='connect-cat'>
<span class='name'>tomatoes</span>
</div>
<div class='connect-cat'>
<span class='name'>tomato</span>
</div>
<div class='connect-cat'>
<span class='name'>tomatos</span>
</div>
<div class='connect-cat'>
<span class='name'>apple</span>
</div>
Edit
Just to reinforce and contrary to what the other answer states, the flag
g
IT DOES HAVE TO DO WITH with the form that the .test()
works, as per
can be seen in this documentation, that I report again. It seems to me that the
our friend of the other answer did not read, or read and did not understand.
Also as per this answer in the Soen:
When you use a global flag on a JS Regexp the "test" and "exec"
each methods Halt at the first match but Keep a Pointer to Where they
stopped Searching in the string. That Pointer can be inspected on the
lastIndex Property. When you call "test" or "exec" Again it Begins
Searching for a match Starting at the lastIndex.
So, when you test a Regexp on a string that Matches the entire string
the lastIndex is set to the end of the string. The next time you test
it Starts at the end of the string, Returns false, and sets lastIndex
back to zero.
Free translation:
When you use the global flag (g
) in a Javascript regex, both
"test" and "exec" methods stop at first match and maintain
this position in the string search. This position can be checked in the
lastIndex property. When you call the "test" or "exec"
again, the search will start from the last verified position (cyto:
This explains the malfunction that was occurring).
So when you test a Regexp on a string that will match the whole
string, lastIndex will be at the end of the string. The next time you
test, will start at the end of the string, returning false, and
redefining lastIndex back to zero.
@dvd so you usually think about voting for people and the empathy you have for them and not the content and quality of the content? Interesting to know.
– Guilherme Nascimento
@dvd where exactly not working, I would be very grateful if you explained where failed, is that difference in Firefox, or failed in Internet explorer?
– Guilherme Nascimento
@dvd What I answered was what I understood of what the AP wanted (which actually is not very clear), I understood that should not appear Omatoes when typing Omato, so it seems to me that the typed word would have to be exact, but if not this I will accept your criticism yes, because if the understanding was wrong on my part it is very fair and praiseworthy to criticize and receive criticism ;)
– Guilherme Nascimento