Javascript 如何检查字符串是否包含数组值?
我花了几个月的时间停止编写代码,现在我正试图重新开始 我正在为一个facebook游戏编写一个用户脚本,该脚本将从与游戏相关的所有帖子中收集数据,并使用这些数据启动接受过程 然而,我试图为用户提供一个选项,让他们选择他们更愿意接受的奖金类型,我在应用它时遇到了困难 我要做的是获取帖子标题字符串,并将其与所选选项数组(复选框值)进行比较,以查看帖子标题是否包含该数组中的任何值 这是“选项”面板,用户可以在其中选择要接受的帖子:Javascript 如何检查字符串是否包含数组值?,javascript,jquery,arrays,string,compare,Javascript,Jquery,Arrays,String,Compare,我花了几个月的时间停止编写代码,现在我正试图重新开始 我正在为一个facebook游戏编写一个用户脚本,该脚本将从与游戏相关的所有帖子中收集数据,并使用这些数据启动接受过程 然而,我试图为用户提供一个选项,让他们选择他们更愿意接受的奖金类型,我在应用它时遇到了困难 我要做的是获取帖子标题字符串,并将其与所选选项数组(复选框值)进行比较,以查看帖子标题是否包含该数组中的任何值 这是“选项”面板,用户可以在其中选择要接受的帖子: $("#rightCol").prepend('<div id=
$("#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>');
我现在想做的是,获取posts标题字符串,检查它是否包含任何数组值,但是我在这样做时遇到了问题
任何帮助都将不胜感激,并提前表示感谢 尝试Array.join和String.search方法:
var re = new RegExp('(' + bonus.join('|') + ')');
console.log('Liam Allan is need of heroic companions!'.search(re) != -1); // true if contains
从您如何填写奖金变量开始:
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
});
例如:
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");
});
会回来吗
heroic companions! found in title
你需要看看array.indexOf-worked for me@Archer我尝试过使用indexOf,但是我的数组值只包含部分字符串,它们并不完全相同
heroic companions! found in title