[bug ?] strange difference between global search and column search

[bug ?] strange difference between global search and column search

trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2
edited September 2016 in Free community support

Hello,

I think I may have discovered a bug in global search... or there is something i really can't understand...
Please have a look at this example page :
https://datatables.net/examples/api/regex.html
Check the checkbox "Treat as regex" and UNcheck "use smart search" for both "Global Search" and "Column - Position".

Try to write nt$ in the "Column - Position" input field and it shows rows with "Accountant", "Junior Technical Author", ... which seems right.
Now, empty the field "Column - Position" and write the same think : nt$ in the "Global Search" input field ==> the table is empty now... Why ??

Thank you very much for your help...

T.

Answers

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    The global search works on a string concatenation of all columns. So $ for the end would only match the last column's data, not some arbitrary point in the string. \b could be used to match the end of a column's data.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    OK I understand. But then, how can I make a global search to find the rows where a CELL ends by "nt" ? The regexp "nt\b" also matches cells which contains "sdfnt sdfsdf" and I don't want this...

    Thanks again !

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    Try nt (double space). The column search strings are concatenated using a double space. The only other option is to use a custom search.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Well... Sorry : regexp are quite new for me. If I would like to match only rows which contains cells which ENDS by nt, what regexp should I use ? (and same question for "BEGINS with nt")...

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    /nt[ {2}\b]/ should do it (remove the leading and trailing /'s - I included them just to be clear that's a regular expression.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Thank you very much but... it does not seems to validate what it should. Please have a look here : https://regex101.com/r/zV7aS3/1
    According to what I understood, the regexp should find "nt5" and "nt8xxxx" but it finds only "nt2" and does not find "nt5" nor "nt8xxxx"...

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    I don't think it should find nt5 or nt8xxxx. You said above:

    contains cells which ENDS by nt

    Neither of the above two end in nt, so it doesn't match them (odd that it matches nt2).

    You might want to consider asking someone with more regex expertise than myself!

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Oups, sorry... I make that mistake because I will have the same question for finding a regexp which match a row if contains a cell which begins with nt.

    Here, please have a look to https://regex101.com/r/zV7aS3/2
    Your regexp matches "aménagement" and "bont" but should'nt (amho) since it's not followed by two spaces... The only word I expect regexp to match would be "rangement" in this situation, isn't it ?

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28

    In this case nt(\s{2}) should do the trick.

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Thank you ! I disabled Smart Search and tried both suggestions in the global search at the top of the table : it does not seems to work...

    Using Allan's suggestion [ {2}\b] first : nt[ {2}\b] finds a row with a cell "BROL-LAURENT (word)". The regexp ux[ {2}\b] also shows a row with a cell "BERTAUX another word". I don't want this because nt / ux does not ends the cells...

    Using F12Magic suggestion (\s{2}) now : nt(\s{2}) and ux(\s{2}) does not show the previous problematic rows (which seems good) but "rk(\s{2})" does not show a row which contains a cell ending by "Mark"...

    Allan, are you really sure that the columns are concatenated with double space for global search ? I'm using 1.10.7.

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin

    Quite certain.

    The regexp ux[ {2}\b] also shows a row with a cell "BERTAUX another word".

    Weird - it shouldn't. Running "BERTAUX another word".match( /ux[ {2}\b]/ ) on your browser's console will show it doesn't match.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    In my browser's console, it matches (added 'gi' for global search and insensitive) :

    "BERTAUX another word".match( /ux[ {2}\b]/gi )

    It also matches here without gi flags :
    https://regex101.com/r/zV7aS3/3

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28
    edited September 2016

    How about this then: /(ux|\s{2})/gi

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Well, it also matches all double spaces " " ; just have a look here : https://regex101.com/r/zV7aS3/4

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28

    Don't know if the following is completely correct regex, but it does seem to do the job you want to accomplish.

    Try: (nt)(?(1)\s{2}|.)|nt$

    https://regex101.com/r/zV7aS3/6

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2
    edited September 2016

    Yes, it seems :-) I will try to put it inside my code to use it with my datatable and I will tell you...
    Now I will look for a regexp which will look for a given string at the beginning of a cell in global search. Thus if I understand correctly what Allan is saying, if I search all cells which begins by "BER", the regexp should match "BER" and " BER" (two spaces followed by BER" but not " BER" (one space followed by BER). Could you please help me ?

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28

    Hi,
    that seems to be a bit easier. How about ^(ber)|(\s{2}ber)
    https://regex101.com/r/zI5bG9/1

This discussion has been closed.