0

Recently I've been writing a script to loop through a a group of elements for certain words, and while the script works in general, I'd like to adapt it to work with wildcard functionality, I've been looking at wildcard usage in JQuery selectors, but when I tried it, it didn't work for me.

Below is the current code I have, I'd be grateful for any advice you may have helping to resolve this.

   var WordToSearch = [
      "word one","word two","word three"
   ];
   WordToSearch.forEach(function(keyword) {
        Word = $("ul#items li:contains('"+keyword+"')").length;
   });

Below is a link to a SO article I found when I made a search for an answer, but when I tried the suggestion(s) I couldn't make it work.

Wildcards in jQuery selectors

Edited to include example input and expected output

Currently the above code will search through the array and highlight the elements that match the terms set in the array.

So a search for "wordone" will return the element <li>I'm looking for the wordone list item</li>.

However I'd like to rewrite it so that I could enter "wordt" and it would return the following elements;

   <li>I'm looking for the wordtwo list item</li>
   <li>I'm looking for the wordthree list item</li>
AlbrightK
  • 73
  • 8
  • Not really clear what you expect from this. Your *"wildcard"* search criteria is not well defined. Provide a sample of input and expected results – charlietfl Jan 01 '17 at 19:14
  • Edited the question to include example input and expected output. – AlbrightK Jan 01 '17 at 21:37
  • The last two elements would already be returned by what you have, no? – trincot Jan 01 '17 at 21:40
  • If you want to highlight matches with a wildcard you can use the `markRegExp` method of [mark.js](https://markjs.io) – dude Jan 01 '17 at 22:33

0 Answers0