如何在字符串数组中搜索字符串

时间:2022-03-28 07:08:53

After searching for an answer in other posts, I felt I have to ask this. I looked at How do I check if an array includes an object in JavaScript? and Best way to find if an item is in a JavaScript array? and couldn't get the code there to work.

在其他帖子中寻找答案后,我觉得我必须问这个问题。我研究了如何检查数组是否包含JavaScript中的对象?找到一个项目是否在JavaScript数组中的最佳方法?无法让代码运行。

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

我在一个数组中捕获一个html嵌入代码,所以每个条目都是一行html代码。

for example:

例如:

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.

我要搜索这个数组,找到单词“height”所在的那条线。

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

理想情况下,我想编写一个函数,返回出现“height”的项的索引。

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

幸运的是,在这个数组中没有副本,所以只有一个发生。

with simple object search I get 'false' returns.

通过简单的对象搜索,我得到了“false”返回。

Any idea?

任何想法?

4 个解决方案

#1


48  

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

它就像迭代数组并查找regexp一样简单

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

Edit - make str as an argument to function.

编辑-使str成为一个函数的参数。

#2


11  

Extending the contains function you linked to:

扩展你所连结的“包含”功能:

containsRegex(a, regex){
  for(var i = 0; i < a.length; i++) {
    if(a[i].search(regex) > -1){
      return i;
    }
  }
  return -1;
}

Then you call the function with an array of strings and a regex, in your case to look for height:

然后用字符串数组和regex调用函数,在这里查找高度:

containsRegex([ '<param name=\"bgcolor\" value=\"#FFFFFF\" />', 'sdafkdf' ], /height/)

You could additionally also return the index where height was found:

您还可以返回找到高度的索引:

containsRegex(a, regex){
  for(var i = 0; i < a.length; i++) {
    int pos = a[i].search(regex);
    if(pos > -1){
      return [i, pos];
    }
  }
  return null;
}

#3


6  

You can use Array.prototype.find function in javascript. Array find MDN.

您可以使用Array.prototype。发现在javascript函数。找到MDN数组。

So to find string in array of string, the code becomes very simple. Plus as browser implementation, it will provide good performance.

为了在字符串数组中找到字符串,代码变得非常简单。此外,作为浏览器实现,它将提供良好的性能。

Ex.

前女友。

var strs = ['abc', 'def', 'ghi', 'jkl', 'mno'];
var value = 'abc';
strs.find(
    function(str) {
        return str == value;
    }
);

or using lambda expression it will become much shorter

或者用lambda表达式,它会变得更短

var strs = ['abc', 'def', 'ghi', 'jkl', 'mno'];
var value = 'abc';
strs.find((str) => str === value);

#4


3  

It's faster to avoid using regular expressions, if you're just trying to find the first substring match within an array of string values. You can add your own array searching function:

如果您只是试图在字符串值数组中找到第一个子字符串匹配,那么避免使用正则表达式会更快。可以添加自己的数组搜索功能:

Code:

代码:

Array.prototype.findFirstSubstring = function(s) {
            for(var i = 0; i < this.length;i++)
            {
                if(this[i].indexOf(s) !== -1)
                    return i;
            }
            return -1;
        };

Usage:

用法:

i.findFirstSubstring('height');

Returns:

返回:

-1 if not found or the array index of the first substring occurrence if it is found (in your case would be 2)

-1如果没有找到,或者第一个子字符串出现的数组索引(在您的例子中是2)

#1


48  

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

它就像迭代数组并查找regexp一样简单

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

Edit - make str as an argument to function.

编辑-使str成为一个函数的参数。

#2


11  

Extending the contains function you linked to:

扩展你所连结的“包含”功能:

containsRegex(a, regex){
  for(var i = 0; i < a.length; i++) {
    if(a[i].search(regex) > -1){
      return i;
    }
  }
  return -1;
}

Then you call the function with an array of strings and a regex, in your case to look for height:

然后用字符串数组和regex调用函数,在这里查找高度:

containsRegex([ '<param name=\"bgcolor\" value=\"#FFFFFF\" />', 'sdafkdf' ], /height/)

You could additionally also return the index where height was found:

您还可以返回找到高度的索引:

containsRegex(a, regex){
  for(var i = 0; i < a.length; i++) {
    int pos = a[i].search(regex);
    if(pos > -1){
      return [i, pos];
    }
  }
  return null;
}

#3


6  

You can use Array.prototype.find function in javascript. Array find MDN.

您可以使用Array.prototype。发现在javascript函数。找到MDN数组。

So to find string in array of string, the code becomes very simple. Plus as browser implementation, it will provide good performance.

为了在字符串数组中找到字符串,代码变得非常简单。此外,作为浏览器实现,它将提供良好的性能。

Ex.

前女友。

var strs = ['abc', 'def', 'ghi', 'jkl', 'mno'];
var value = 'abc';
strs.find(
    function(str) {
        return str == value;
    }
);

or using lambda expression it will become much shorter

或者用lambda表达式,它会变得更短

var strs = ['abc', 'def', 'ghi', 'jkl', 'mno'];
var value = 'abc';
strs.find((str) => str === value);

#4


3  

It's faster to avoid using regular expressions, if you're just trying to find the first substring match within an array of string values. You can add your own array searching function:

如果您只是试图在字符串值数组中找到第一个子字符串匹配,那么避免使用正则表达式会更快。可以添加自己的数组搜索功能:

Code:

代码:

Array.prototype.findFirstSubstring = function(s) {
            for(var i = 0; i < this.length;i++)
            {
                if(this[i].indexOf(s) !== -1)
                    return i;
            }
            return -1;
        };

Usage:

用法:

i.findFirstSubstring('height');

Returns:

返回:

-1 if not found or the array index of the first substring occurrence if it is found (in your case would be 2)

-1如果没有找到,或者第一个子字符串出现的数组索引(在您的例子中是2)