Regex Include/Exclude Searches simply?

Regex Include/Exclude Searches simply?

JaySmithJaySmith Posts: 3Questions: 1Answers: 0
edited April 21 in Free community support

Hey Allan
I've been using exclusion searches using a negative look ahead regex and client-side processing for quite some time. Now, I'm trying to adapt existing regex to include positive/negative matches using the global search input. This allows users to enter a search either as usual (e.g. this that "something else") and use the smart search regex to find the matches, or to use a -/+ search (e.g. -this|that +something|something else ) to conduct, essentially, boolean searches using a list of ORd terms.

The code is quite simple:

  function filterGlobal () {
    var global_filter = $('.dataTables_filter input');
    if ( global_filter.val().toString().charAt(0) == '-') {
      splits = global_filter.val().toString().split('+');
      var negatives = splits[0].substr(1).trim();
      var positives = splits[1] ? splits[1].trim() :'';
      var searchterm = '^(?:(?!(' + negatives + ')).)*' + (positives ? '(?:(?=(' + positives + ')).)*' : '') + '$';
      console.log(searchterm);
      $('#products-table').DataTable()
        .search(
          searchterm,
          true,
          false
        )
        .draw();
    } else {
      $('#products-table').DataTable()
        .search(
          global_filter.val(),
          false,
          true
        )
        .draw();
    }
  }

This code, called on keyup event, creates regex like:

^(?:(?!(this|that)).)*(?:(?=(another|thing)).)*$

And this pattern matches in Javascript pattern testing ( such as https://regexr.com/4clct ) against /g/m text.

The negative lookaheads work, but the positive lookaheads don't work in datatables.js.

Any idea why? Recommendations?

Answers

  • colincolin Posts: 4,691Questions: 0Answers: 822

    Hi @JaySmith ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • JaySmithJaySmith Posts: 3Questions: 1Answers: 0
    edited April 24

    Here's the "test case" that you request. Along with it you'll find a second sandbox for regex testing, which uses the output from the simple builder routine to verify that the regex sent to datatables match is accurately constructed to achieve the desired result. It does.

    To test in the datatables sandbox, enter the following (no quotes) in the global search box:
    "-bruce +silva".

    This input correctly formats a regex that uses positive and negative lookaheads and in the regex JS sandbox correctly selects all lines containing the "+terms" and correctly excludes the lines containing the "-terms". The datatables regex filter fails to properly exclude NON matching +term positive lookaheads, both with and without capture groups. Why?

    Just to be clear, I'm not really asking for help as much as I am for an explanation. I'm comfortable with my proficiency with all involved client and server-side scripting. I'm asking for explanations because behavior using your code is inconsistent with behavior using pure Javascript and in testing sandboxes. My hope is that you'd want to know the answer to this question as much as I wish to elicit one.

    To test the regex in datatables.js environment:

    http://live.datatables.net/reziyuno/1/edit?html,css,js,console,output

    To test the regex output in JS:

    https://regexr.com/4cprj

    Thanks!

  • JaySmithJaySmith Posts: 3Questions: 1Answers: 0

    Hey Colin

    Solved. We had implemented the wrong positive lookahead, which was not an exclusive regex. So in JS and datatables match the results were wrong.

    With the correction in place all things are now working as expected.

    Thanks!!!

  • kthorngrenkthorngren Posts: 5,542Questions: 19Answers: 1,232

    What change did you make?

    Kevin

Sign In or Register to comment.