jQuery的:查找文本字符串数组项 [英] jQuery: Find array items in text string
本文介绍了jQuery的:查找文本字符串数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何才能找到一个文本字符串数组项?
How can I find array items in a text string?
我不知道数组,我不知道该文本。但是,当一个数组项包含在文本,然后派对!
I don't know the array and I don't know the text. But when an array item is contained in the text then party!
var arrayString = 'apple | ape | soap',
text = "This a nice apple tree.";
var array = arrayString.split(" | ");
var matchedArrayItem = "..."; // please help on this
if(matchedArrayItem) {
$("body").append('The text contains the array item "'+ matchedArrayItem +'".');
}
测试: http://jsfiddle.net/9RxvM/
推荐答案
使用JavaScript的搜索(STR)
。
Use JavaScript search(str)
.
var matchedArrayItem = "";
for(var i=0;i<array.length;i++){
if(text.search(array[i])!=-1){
matchedArrayItem = array[i];
break;
}
}
if(matchedArrayItem!="") {
$("body").append('The text contains the array item "'+ matchedArrayItem +'".');
}
请注意,这将得到数组中的第一个匹配的项目。
要检查是否有匹配的项目,只是检查是否matchedArrayItem =;!
Note that this will get the first matched item in the array. To check if there is a matched item, just check if matchedArrayItem!="";
这篇关于jQuery的:查找文本字符串数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文