2

I have a code for finding multiple words in a table but it's not yet perfect. what if i inputted those multiple words not in order? for example the "John Lennon 151 sf" and "paul mccartney 753 tj" and i typed the word "lennon 753 tj" the result should be the two of them. but the result that i received is the row of john lennon only..

here's my code

<input id="emp_search" />
<input id="search" type="button" value="search" />
<table>
   <tr>
       <th>First Name</th>
       <th>Last Name</th>
       <th>number</th>
       <th>letters</th>
   </tr>
   <tr>
       <td>John</td>
       <td>Lennon</td>
       <td>151</td>
       <td>sf</td>
   </tr>
   <tr>
       <td>Paul</td>
       <td>McCartney</td>
       <td>753</td>
       <td>tj</td>
   </tr>
   <tr>
       <td>George</td>
       <td>Harrison</td>
       <td>24</td>
       <td>ge</td>
   </tr>
   <tr>
       <td>Ringo</td>
       <td>Starr</td>
       <td>26</td>
       <td>hg</td>
   </tr>
</table>

and here is..

if (!RegExp.escape) {
    RegExp.escape = function (s) {
        return s.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
    };
}
jQuery(function ($) {
    ///search this table
    $(' #search ').click(function () {
        var searchthis = new RegExp($(' #emp_search ').val().replace(/\s+/, '|'), 'i');
        $("table").find("tr").slice(1).each(function (index) {
            var text = $.trim($(this).text());
            $(this).toggle(searchthis.test(text));
        });
    });
});

and what will i add to my jquery? thanks in advance :)

http://jsfiddle.net/wind_chime18/ANLgD/5/

Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43
Vincent
  • 852
  • 6
  • 29
  • 67
  • 1
    I think it was solved in your previous post itself http://jsfiddle.net/arunpjohny/D6nzC/10/ – Arun P Johny Nov 26 '13 at 07:02
  • 1
    @arun it's not working when i want to search one person. and also the band name beatles. like "John beatles" – Vincent Nov 26 '13 at 07:28
  • @Vincent if you search for `John Beatles` it will display all the rows as `Beatles` is present in all of them http://jsfiddle.net/arunpjohny/D6nzC/15/ – Arun P Johny Nov 26 '13 at 07:32

2 Answers2

3

Your replace gives : /lennon|753 tj/i. Use the global flag :

$('#emp_search').val().replace(/\s+/g, '|')

Moreover, don't forget to trim the input ("lennon " currently gives /lennon|/i) :

$.trim($('#emp_search').val()).replace(/\s+/g, '|')

Here is a demo which actually works : http://jsfiddle.net/wared/93jEY/.

Finally, you have forgotten to use escape :

RegExp.escape($.trim($('#emp_search').val()).replace(/\s+/g, '|'))
  • what if a i have more column containing all beatles. i want to search paul beatles. the result should be only paul mccartney – Vincent Nov 26 '13 at 07:52
  • "the result should be the two of them", why this sentence should not apply to the band? –  Nov 26 '13 at 09:11
  • because. what if..... i have the same first and last name but different band name – Vincent Nov 26 '13 at 09:17
1

Try this

Problem is in your regex .replace(/\s+/, '|')

Replace this .replace(/\s+/, '|') by this .replace(/ /g,"|")

Fixed fiddle : Demo

Replacing spaces with underscores in JavaScript?

Edit : Forgot to update the demo.... now I updated

Community
  • 1
  • 1
rynhe
  • 2,509
  • 1
  • 21
  • 27
  • how about this? http://jsfiddle.net/wind_chime18/ANLgD/8/ with all the same band name. beatles but if i searched "john starr beatles" – Vincent Nov 26 '13 at 07:27
  • the result should be 2 rows – Vincent Nov 26 '13 at 07:40
  • "the result should be the two of them", why this sentence should not apply to the band? –  Nov 26 '13 at 07:43
  • Well :) Now, try the above demo with "lennon ", then check this suggestion `.replace(/ /g,"|")` with "lennon paul" (add several whitespaces between the names). Both does not work. –  Nov 26 '13 at 07:55
  • im having also a problem. with Male and Female, when i tried to search the male , the female is also displayed, because the female has "male" characters in it. – Vincent Nov 26 '13 at 09:38
  • 1
    @rynhe try searching "male" – Vincent Nov 26 '13 at 09:41
  • 2
    @Vincent This situation (searching for "male") works as expected. It's hard to help if you change your direction everytime. Resolve the actual problem (make a choice between AND and OR), close this topic, then post a new question ("how to search for "begins with"?"). –  Nov 26 '13 at 10:09
  • 1
    oh im sorry.. i was just thinking of the possible results. – Vincent Nov 26 '13 at 11:38
  • 1
    @Vincent What are you talking about now :D You think about things? You see dead people? Take it easy, I'm joking. I mean, try to focus on one problem at a time, otherwise this discussion will never end ;) –  Nov 26 '13 at 11:57
  • 1
    hahaha. ok. i'm going to create a new post. with this male and female thing haha – Vincent Nov 26 '13 at 12:33
  • 1
    Try to resolve this problem by yourself before posting a new question, otherwise you're likely to be downvoted. Hint to search for leading chars : `/^lennon/i` (`^` stands for "begins with"). –  Nov 26 '13 at 12:55
  • well i've tried that. still not working :( I'm just new in programming to be honest. – Vincent Nov 26 '13 at 13:24
  • @Vincent Well, hold on, post a new question with something you've tried :) –  Nov 26 '13 at 14:32
  • @wared here is my new post. http://stackoverflow.com/questions/20217409/search-in-a-table-with-multiple-words/ – Vincent Nov 27 '13 at 00:47