locked
how to use like search RRS feed

  • Question

  • User-1634604574 posted

    i have this code to search through this string but i don't have result

            let a ="kool" 
    var term = 'oo'; 
    var search = new RegExp(term , 'i'); 
    let b = a.filter(item => search.test(item));
    alert(b); 

    Thursday, May 21, 2020 6:13 AM

All replies

  • User-719153870 posted

    Hi zhyanadil,

    The filter method in JS is used to process arrays, and cannot operate on strings.

    Press F12 to open the console and you will see the problem.

    If you want to match the string, you can do like this:

    let a = "kool"
    var term = 'oo';
    var search = new RegExp(term, 'i');
    let b = search.test(a);
    alert(b);
    

    Hope this can help you.

    Best Regard,

    Yang Shen

    Thursday, May 21, 2020 7:37 AM
  • User-474980206 posted

    its not clear what you expect "b" to be

       /oo/i.test("kool"); // true

      "kool".indexOf("oo"); // 1

      "kool".match(/oo/i)  // ["oo"]

      "kool".search(/oo/i); // 1

    Thursday, May 21, 2020 8:17 PM