如何搜索字符串的字符串数组内

编程入门 行业动态 更新时间:2024-10-26 22:19:40
本文介绍了如何搜索字符串的字符串数组内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

寻找其他职位的答案之后,我觉得我要问这个。我看着在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.

更多推荐

如何搜索字符串的字符串数组内

本文发布于:2023-10-11 13:51:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!