How to check if string contains array values?

i took a little break from coding for a few months, and now i am trying to get back into it again.

I am working on a userscript for a facebook game, which will collect data from all posts related to the game, and use that data to start the accepting process.

However, i am trying to introduce an option for users to select which bonus types they would prefer to accept, and im having trouble applying it.

What i want to do is take the posts title string, and compare it to an array of selected options (checkbox values) to see if the post title contains any values from the array.

This is the options panel where users can select which posts to accept:

$("#rightCol").prepend('<div id="bonus_options_panel">'
                       +'<ul id="bonuses"><legend><b>Select Bonuses to accept...</b></legend>'
                       +'<li><label>Hungry No More <input type="checkbox" value="heroic companions!" class="bonus_select_option"></label></li>'
                       +'<li><label>Pikemen <input type="checkbox" value="extra pikes!" class="bonus_select_option"></label></li>'
                       +'<li><label>Swordsmen <input type="checkbox" value="recruited swordsmen!" class="bonus_select_option"></label></li>'
                       +'<li><label>Valuable Treasure <input type="checkbox" value="valuable treasure!" class="bonus_select_option"></label></li>'
                       +'<li><label>Jousting Competition <input type="checkbox" value="jousting game!" class="bonus_select_option"></label></li>'
                       +'<li><label>Gamble <input type="checkbox" value="wants to gamble." class="bonus_select_option"></label></li>'
                       +'<li><label>Extra Lumber <input type="checkbox" value="has extra lumber!" class="bonus_select_option"></label></li>'
                       +'<li><label>Foreign Traders <input type="checkbox" value="attracted foreign traders!" class="bonus_select_option"></label></li>'
                       +'<li><label>Buxom Wenches <input type="checkbox" value="inviting friends over!" class="bonus_select_option"></label></li>'
                       +'<li><label>Plague <input type="checkbox" value="kingdom has the plague!" class="bonus_select_option"></label></li>'
                       +'<li><label>Legendary Bard <input type="checkbox" value="hosting a legendary bard!" class="bonus_select_option"></label></li>'
                       +'<li><label>Wild Horses <input type="checkbox" value="found wild horses!" class="bonus_select_option"></label></li>'
                       +'<li><label>Mercenary Armies <input type="checkbox" value="mercenary armies with you." class="bonus_select_option"></label></li>'
                       +'<li><label>Famed Hero <input type="checkbox" value="feasting with a famed hero!" class="bonus_select_option"></label></li>'
                       +'<li><label>Queen Parading <input type="checkbox" value="Queen is parading!" class="bonus_select_option"></label></li>'
                       +'<li><label>Wanted Criminal <input type="checkbox" value="caught a wanted criminal!" class="bonus_select_option"></label></li>'
                       +'<li><label>Rolling Logs <input type="checkbox" value="has extra logs!" class="bonus_select_option"></label></li>'
                       +'<li><label>Archery Game <input type="checkbox" value="holding a Archery game!" class="bonus_select_option"></label></li>'
                       +'</ul>'
                       +'</div>');

and here is an example of a posts title string: Liam Allan is need of heroic companions!

i have been creating an array of checkbox values like so:

var bonus = [];
$("input.bonus_select_option:checked").each(function(){
     bonus.push($(this).val());
 });

What i would like to do now is, take the posts title string, and check to see if it contains any of the array values, but im having trouble doing so.

Any help would be greatly appreciated and thanks in advance!

Answers


Try Array.join and String.search methods:

var re = new RegExp('(' + bonus.join('|') + ')');
console.log('Liam Allan is need of heroic companions!'.search(re) != -1);  //  true  if contains

Starting with how you filled your bonus variable:

  var title = 'Liam Allan is need of heroic companions!';
  var bonus = [];
  $("input.bonus_select_option:checked").each(function(){
        bonus.push($(this).val());
  });
  $.each(bonus, function(key, value){
        if(title.indexOf(value) > 0) ... //Do something 
  });

For example:

  var title = 'Liam Allan is need of heroic companions!';
  var bonus = [];
  bonus.push('heroic companions!');
  bonus.push('recruited swordsmen!');
  bonus.push('feasting with a famed hero!');

  $.each(bonus, function(key, value){
    if(title.indexOf(value) > 0) alert(value + " found in title");
  });

would return

  heroic companions! found in title

Need Your Help

Should I be terminating a forked child process with exit()?

c fork parent-child

I'm working on some stuff using fork() in C. This is my first contact with the concept of forking processes.

Why the response details vary in case of Firefox and Chrome browser?

google-chrome firefox asp.net-mvc-2 css-sprites

I am working on implementation of CSS Sprite for asp.net mvc2 application. I followed the below article and tried to work on a sample project.