Regex $ (end of string) works for column search but not global search

Regex $ (end of string) works for column search but not global search

bg7bg7 Posts: 44Questions: 6Answers: 0

Link to test case:
https://datatables.net/examples/api/regex

Description of problem:
Using the regex example page linked above, if you put the string "on$" in the name column search and select "Treat as regex" you get four results back, as expected. However, if instead you put that same string in the Global Search field above it and select "Treat as regex" you get back no results. Shouldn't the global results include the four that the column search finds? Note that using the start of line character (e.g. searching for "^T") yields the same results for both searches.

Thanks.

Ben

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    That is correct. The global search operates by concatenating all the values into a single string and then running the regex on that. That allows the "smart" search to operate across multiple columns. The downside is that it isn't possible to use $ ending or ^ starting searches for a regex global search.

    Allan

  • bg7bg7 Posts: 44Questions: 6Answers: 0

    Allan,

    Ok, I follow - that means that for the global search you can match the ^ but only against the data in the first column and match the $ but only against the data in the last column. That works in the example page. Should the global search be able to match the start and end against the data in any column if you turn the smart search off? It doesn't in the example page but I could have sworn this used to work and we've got this set as our config:

    search: {
    regex: true,
    smart: false
    },

    Thanks.

    Ben

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited April 2023 Answer ✓

    I believe what happens is the row filter values are joined with two spaces to separate the columns, for example:

    Ashton Cox  Technical Author  San Francisco  66  2009/01/12  $4,800
    

    This string is used to the global search no matter which mode it is in. I don't recall this being different in the past and remember past threads with the same questions and answers.

    Possibly you can use \b (word boundary) to filter based on column boundaries with the global search. For example search for \bSan Francisco\b in the Global search input with regex on and smart search off.

    Here is an example:
    https://live.datatables.net/gugecuca/1/edit

    Cedric Kelly is displayed even because the Position column has San Francisco.

    Kevin

  • bg7bg7 Posts: 44Questions: 6Answers: 0

    Kevin,

    I must be misremembering then. Thanks for the workaround suggestion. It looks like you can also improvise by using two spaces as part of the search text to match the boundary between columns.

    Thanks.

    Ben

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    This is correct yes. The smart search being disabled doesn't stop DataTables from concat'ing the values into a single string - it just stops the DataTables regex that allows words to be in any order from being constructed.

    A boundary condition as Kevin suggests is usually the best way. A double space as you have found is an option as well, although it isn't perfect, since the source data could also have a double space in some data values.

    Allan

Sign In or Register to comment.