How to replace " | " with a " , " in the regex search?

How to replace " | " with a " , " in the regex search?

MoizMoiz Posts: 32Questions: 5Answers: 1

Hi,
First of all, I would like to appreciate the whole team's effort. Secondly, I am using regex on global search which is working fine. So the problem I'm facing is that by default we have to type multiple values in the search input field like this Tokyo|London|San Francisco which I don't want. What I want is to replace " | " with " , " in the search input field. For instance: Tokyo, London, San Francisco and do the same work. I hope you guys get my point. Have a nice day. :)

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Hi @Moiz ,

    Thanks for the kind words.

    That's not a valid regex, so you'll need to convert the commas to bars first (replace(/,/g, '|')). Probably the easiest way would be to repurpose the existing search input, as shown in this example.

    Cheers,

    Colin

  • MoizMoiz Posts: 32Questions: 5Answers: 1

    Thanks for helping me out brother @colin! I have implemented this in my project and it works like a charm but the problem I'm facing right now is that after searching multiple values when I click on arrows for sorting, it changes back the string of search input field to the default format which I don't want. For instance: Tokyo|London|New York
    This is the code I have added.

    $('.dataTables_filter input').on( 'keyup click', function () {

    var str = $(this).val();
    str = str.replace(/,/g, '|');
    table.search(str).draw();
    

    } );

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @Moiz ,

    Could you update my test case to demonstrate that, please.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    it changes back the string of search input field to the default format which I don't want. For instance: Tokyo|London|New York

    I would expect that to happen. If you don't want that then you will need to create your own search input and use the dom option to remove the default search input.

    Kevin

  • MoizMoiz Posts: 32Questions: 5Answers: 1
  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Ah, yep, as Kevin said. The change of order causes a draw which means the search term (with the bars) is pushed into the input element. If you use your own input element as Kevin said, this wont happen. Apologies for the confusion.

  • MoizMoiz Posts: 32Questions: 5Answers: 1

    Hmm. Okay, brother Kevin, I will also try that and give you my feedback. :)

  • MoizMoiz Posts: 32Questions: 5Answers: 1

    Nah! Don't Apologize to me brother @colin. Again thanks for helping me out both of you. Cheers :)

This discussion has been closed.