How do you search a whole string for whole terms?

How do you search a whole string for whole terms?

vismarkvismark Posts: 79Questions: 5Answers: 0

I'm using a multiple input type text search with "|" as separator, this is the code:

table.columns(1).search($(this).val(), true, true).draw();
I've noticed that the first term is being searched in the whole string, from the second term i will only have a match at the beginning of the string.

A practical case:

In this column i have:

TEGR1501
[...]
TEGR2001
typing "1501|2001" will only return "TEGR1501" while typing "1501|TEGR2001" is returning both "TEGR1501" and "TEGR2001".

How can i search the whole string for the whole terms?

Replies

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    edited October 2016

    The documentation for .search says not to use "true, true" parameters in combination. One of those should be false.

    I don't like requiring my users to remember the pipe symbol, so I use this to make spaces work like pipes.

        val = $(this).val();
           //remove leading white space so reg ex doesn't get confused
        val = val.replace(/^\s/g,"");                               
        //remove trailing white space so reg ex doesn't get confused
        val = val.replace(/\s$/g,"");                                   
        //convert white space to pipes
        val = val.replace(/\s/g,"|");                           
        if ( column.search() !== this.value ) {
            column
             .search(  val  , true, false )
            .draw();
        }
    

    I'm not sure how to get whole words. In my quick testing, the \b markup in a reg ex should be a boundary indicator, but DT doesn't like it.

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    I should have searched for this (you should have too :) ) - The answer was the first thing that came up in the search.

    This works for a whole word search, with space separators. I needed to escape my \b entries.

    $( 'input', this.header() ).on( 'keyup change', function () {
        if ( $(this).val() == undefined) {
            val = "";
        } else {
            val = $(this).val();
        }
            //remove leading white space so reg ex doesn't get confused
        val = val.replace(/^\s/g,"");                               
        //remove trailing white space so reg ex doesn't get confused
        val = val.replace(/\s$/g,"");       
        if (val.length > 0 ) {
        //convert white space to pipes
            val = val.replace(/\s/g,"\\b|\\b"); 
            val = "\\b" + val + "\\b";
        }
        if ( column.search() !== this.value ) {
            column
                .search(  val  , true, false )
                .draw();
        }
    } );
    
    
  • vismarkvismark Posts: 79Questions: 5Answers: 0

    Hi thom and thanks for your reply. I maybe express myself in a not clear way and i'm sorry. The method you suggested works only if you type in the whole word for all the therms but i need to filter also by partial word IN ALL TERMS. Problem in fact is that the partial string matches ONLY ON THE FIRST WORD, from the second on the match is fired only if the string match at least the beginning of the strings in the table.

    So i make some new examples:

    This is table content i have:

    TEGR1501
    [...]
    TEGR2001

    actual behaviour (BAD):
    typing "1501|2001" i get only "TEGR1501"
    typing "1501|TEGR2001" i get both "TEGR1501" and "TEGR2001"

    desired behaviour (OK):
    typing "1501|2001" and getting both "TEGR1501" and "TEGR2001"
    as well as
    typing "1501|TEGR2001" and getting both "TEGR1501" and "TEGR2001"
    as well as
    typing "TEGR1501|TEGR2001" and getting both "TEGR1501" and "TEGR2001"

    And thanks for your suggestion on the separator! :)

  • vismarkvismark Posts: 79Questions: 5Answers: 0

    Ouch, the solution was to set the second parameter to false! My bad i didn't read good enough. Thanks for your help Thom! :)

This discussion has been closed.