Want to use both regex search and smart search in my application

Want to use both regex search and smart search in my application

Abhishek_choukseyAbhishek_chouksey Posts: 4Questions: 2Answers: 0

My application is already using smart search in datatable but now I want to impliment multiple string search in each column for example: if i type USER1;USER2 then it should search for the row containing these word. my string also contain special character some time like "(206) 902-3203" or "user-name".
To impliment this functionality I use custom regex where i replace ";" with " |" and perform OR operation it works fine but smart search facility got disable and if I use both regex and smart search enabled then conflicts happen can anyone tell me how can I impliment multiple search string facility without disabling smart search

code:
$j( 'input', $headerCol ).on( 'change input', function () {

            var input1= escapeRegExp(($j( 'input', $headerCol )).val());
            if ( column.search() !== this.value ) {
                input1 = input1.replace(/^;(\s+)?|;(\s+)?$/g,'');
                var keywords = input1.split(';');
                var filter = '';
                for (var i = 0; i < keywords.length; i++) {
                    filter = (filter !== '') ? filter + '|' + $j.trim(keywords[i]) : $j.trim(keywords[i]);
                }
                column.search($j.trim(filter), true, true).draw();

            }
        } );

        function escapeRegExp(string) {
            return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); 

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Not sure I understand what you are trying to do. The search() API docs explain the regex and smart search modes. Maybe you can put together a simple example showing you data and describing exactly what you want the result to be.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    If using search($j.trim(filter), true, true) doesn't work the way you want then maybe you will need to create a Search Plugin to perform the search the way you want.

    Kevin

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    I think it would be really hard to get smart search working this way - it basically wraps the search term in its own regex and you'd ned to take that into account when entering your own regex. Generally speaking, it would very much encourage avoiding using your own regex with the built in smart search.

    However, perhaps using SearchBuilder or SearchPanes would suit you needs for this?

    Thanks,
    Allan

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    I think I just realized what you are asking for. You will want to use regex wildcards as part of the search term. I think you are wanting a search term like this .*USER1.*|.*USER2.*. You can experiment with the search terms at https://regex101.com/ . Also make sure to turn off smart search.

    Kevin

  • Abhishek_choukseyAbhishek_chouksey Posts: 4Questions: 2Answers: 0
    edited October 2020

    @kthorngren Can you please tell me about this .* wild card? and how can i use this in my above mention code?

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    There are lots of websites with regex tutorials like this one:
    https://regexone.com/

    Kevin

  • Abhishek_choukseyAbhishek_chouksey Posts: 4Questions: 2Answers: 0

    @kthorngren
    Hi, is it possible that I can use this custom regex .USER1.|.USER2. along with the smart search feature also enabled. because by using my custom regex I lost the smart search feature and I want to retain it as well.

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Did you try my suggestion of .*USER1.*|.*USER2.*? For example:
    http://live.datatables.net/hujadera/1/edit

    Kevin

This discussion has been closed.