Use indexOf()
with the exact string you're searching for:
$('table td').addClass(function(){
return $(this).text().indexOf('test details') > -1 ? 'matched' : 'unmatched';
});
References:
- JavaScript:
- jQuery:
Pregunta
I'm trying to filter many literal elements in a repeater that match with a filter text input and jQuery. The structure is mainly like this:
<div id="divResults">
<table>
<tr>
<td>My test text </td>
</tr>
<tr>
<td>Another test </td>
</tr>
<tr>
<td>Further text details </td>
</tr>
<tr>
<td>More details </td>
</tr>
<tr>
<td>More about the test details, click here </td>
</tr>
</table>
</div>
The code:
if ($("#div > tr").text().search(new RegExp("test", "i")) < 0){
//hide();
} else {
//show();
}
Everything works great. But the things is when I type "test details" I get all the TD elements that contains the word "test" and "details" but I what I want now is to filter an exact match I mean in my case "test details" would be only the last TD. How can I change this code to filter an exact match?
Solución
Use indexOf()
with the exact string you're searching for:
$('table td').addClass(function(){
return $(this).text().indexOf('test details') > -1 ? 'matched' : 'unmatched';
});
References:
Otros consejos
You can update your Regular Expression to match the phrase:
new RegExp('^(?=.*?test details).*$', 'i')
Example