Question

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?

Was it helpful?

Solution

Use indexOf() with the exact string you're searching for:

$('table td').addClass(function(){
    return $(this).text().indexOf('test details') > -1 ? 'matched' : 'unmatched';
});

JS Fiddle demo;

References:

OTHER TIPS

You can update your Regular Expression to match the phrase:

new RegExp('^(?=.*?test details).*$', 'i')

Example

http://jsfiddle.net/Mj6P6/2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top