how to get exact match with multiple strings in a regex

how to get exact match with multiple strings in a regex

PelluchePelluche Posts: 2Questions: 1Answers: 0
edited May 2017 in Free community support

I have a column with levels such as "C, C+, C-, D...". I have checkboxes for filters that lets you choose multiple boxes. I have it creating a regex that spits out something like "C-|C|C+".

If you click "C" box it shows results with "C", "C+", and "C-". I need to grab just "C". What am I doing wrong?

$('.dataTable').DataTable({               
      "order": [[ 0, 'asc' ]],
       bFilter:true,
       "search": {
            "regex": true
       }                                            
});

$("input[name='tourneyLevel']").on("change", function(e) {
      var levelSelected = [];                                            
      $("input[name='tourneyLevel']:checked").each(function(index, city) {                                                
          levelSelected.push([$(this).val()]);
      });

      var regex = levelSelected.join("|");
      console.log(regex);                                          

      $('.dataTable').DataTable().column(4).search(regex, true, false).draw();
});

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736

    This is more a regex syntax issue than Datatables so you may want to look at some regex tutorials. Can the user choose more than one level? This will determine how you need to build the expressions.

    Not that this solves your specific problem but I think "C-|C|C+" is not going to work as you are expecting. I don't think it will match "C+" because it will match the "|C|" portion first plus you need to escape the "+" with a "\". I believe this statement would need to be this: "C-|C+|C".

    To find only "C" and not C+ or C- you will need something more like this: "C[^+-]{0,1}$". The "^" inside the "[]" excludes the "+" and "-". The "{0,1}" matches 0 or 1 time. The "$" is end of string. This is not a perfect regex as "C" followed by anything other than "+" or "-" will be matched, for example "Cf" will match.

    To match just "C-" your regex will need to be "C-$" but to match "C+" it will need to be "C+$". To match "C" or "C+" you would need something like this" "(?:C[^+-]{0,1}$|C+$)".

    This site is very helpful in helping to build regex expressions interactively:
    https://regex101.com/

    Hopefully this will get you started. You may find these expressions will need to be updated to fit your exact requirements.

    Kevin

  • PelluchePelluche Posts: 2Questions: 1Answers: 0

    yes the user can choose multiple Levels so if they chose C+ and C- it should grab entries with either or. I'll play around with your answer, thank you for the great explanations. Sorry for late response, i just got sidetracked but now this is on my plate to solve.

  • amajithamajith Posts: 3Questions: 1Answers: 0
    edited March 2019

    anyone find solutions?

  • amajithamajith Posts: 3Questions: 1Answers: 0

    i have find solution.

    from here https://stackoverflow.com/a/47680887/6440019

This discussion has been closed.