如何搜索字符串的字符串数组内 [英] How to search for a string inside an array of strings

查看:110
本文介绍了如何搜索字符串的字符串数组内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找其他职位的答案之后,我觉得我要问这个。
我看着在JavaScript中 array.contains(OBJ)
找到一个JavaScript数组中的项目最好的方法? 并不能得到code那里工作。

After searching for an answer in other posts, I felt I have to ask this. I looked at array.contains(obj) in JavaScript and Best way to find an item in a JavaScript array? and couldn't get the code there to work.

我捕捉HTML嵌入code到一个数组,所以每个产品的html code的一条线。

I am capturing an html embed code into an array, so each item is a line of html code.

例如:

i[0]='<param name=\"bgcolor\" value=\"#FFFFFF\" />'
i[1]='<param name=\"width" value=\"640\" />'
i[2]='<param name=\"height\" value=\"360\" />'   
i[3]='more html code' 

我要搜索这个数组并找到行这里所说的'高度'的。

I want to search this array and find the line where the word 'height' is.

所以,理想情况下,我想编写一个返回,其中高度出现在项目的索引功能。

So ideally, I'd like to write a function that returns the index of the item where 'height' appears.

幸运的是,这个数组中没有重复,所以只出现一次。

Fortunately, there are no duplicates in this array, so there's only one occurrence.

用简单的搜索对象,我得到'假'的回报。

with simple object search I get 'false' returns.

任何想法?

推荐答案

这是为遍历数组和寻找的正则表达式简单

It's as simple as iterating the array and looking for the regexp

function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if (strArray[j].match(str)) return j;
    }
    return -1;
}

修改 - 使 STR 作为参数传递给函数

Edit - make str as an argument to function.

这篇关于如何搜索字符串的字符串数组内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆