如何找到以某些字母开头的javaScript数组中的所有元素

时间:2021-11-15 07:10:00

Is there any way to do this filtering out only items in an array that start with the letter a. ie

有没有办法这样做只过滤掉以字母a开头的数组中的项目。即

var fruit = 'apple, orange, apricot'.split(',');
  fruit = $.grep(fruit, function(item, index) {
  return item.indexOf('^a'); 
  });
alert(fruit);

3 个解决方案

#1


Three things:

  • You want to split by ', ', not ','
  • 你想分开','而不是','

  • indexOf doesn't take a regex, but a string, so your code searches for a literal ^. Use search if you want to use regular expressions.
  • indexOf不带正则表达式,而是一个字符串,所以你的代码搜索文字^。如果要使用正则表达式,请使用搜索。

  • indexOf (and search) do return the index where they find the sought-after term. You'll have to compare that to your expectation: == 0. Alternatively, you can use the regex test method which returns a boolean.
  • indexOf(和搜索)确实返回他们找到所需术语的索引。你必须将它与你的期望进行比较:== 0.或者,你可以使用返回布尔值的正则表达式测试方法。

alert('apple, orange, apricot'.split(', ').filter(function(item, index) {
    return item.indexOf('a') == 0; 
}));
alert('apple, orange, apricot'.split(', ').filter(function(item, index) {
    return /^a/.test(item); 
}));

#2


You have to trim the spaces from the item before checking.

在检查之前,您必须修剪项目中的空格。

Regex to check if start with: ^a

正则表达式检查是否以:^ a开头

var fruit = 'apple, orange, apricot'.split(',');
fruit = $.grep(fruit, function (item, index) {
    return item.trim().match(/^a/);
});
alert(fruit);

Other solution:

var fruits = [];
$.each(fruit, function (i, v) {
    if (v.match(/^a/)) {
        fruits.push(v);
    }
});
alert(fruits);

#3


You can use charAt like so :

您可以像这样使用charAt:

var fruit = 'apple, orange, apricot'.split(', ');
  fruit = $.grep(fruit, function(item, index) {
  return item.charAt(0) === 'a';
});
alert(fruit);

#1


Three things:

  • You want to split by ', ', not ','
  • 你想分开','而不是','

  • indexOf doesn't take a regex, but a string, so your code searches for a literal ^. Use search if you want to use regular expressions.
  • indexOf不带正则表达式,而是一个字符串,所以你的代码搜索文字^。如果要使用正则表达式,请使用搜索。

  • indexOf (and search) do return the index where they find the sought-after term. You'll have to compare that to your expectation: == 0. Alternatively, you can use the regex test method which returns a boolean.
  • indexOf(和搜索)确实返回他们找到所需术语的索引。你必须将它与你的期望进行比较:== 0.或者,你可以使用返回布尔值的正则表达式测试方法。

alert('apple, orange, apricot'.split(', ').filter(function(item, index) {
    return item.indexOf('a') == 0; 
}));
alert('apple, orange, apricot'.split(', ').filter(function(item, index) {
    return /^a/.test(item); 
}));

#2


You have to trim the spaces from the item before checking.

在检查之前,您必须修剪项目中的空格。

Regex to check if start with: ^a

正则表达式检查是否以:^ a开头

var fruit = 'apple, orange, apricot'.split(',');
fruit = $.grep(fruit, function (item, index) {
    return item.trim().match(/^a/);
});
alert(fruit);

Other solution:

var fruits = [];
$.each(fruit, function (i, v) {
    if (v.match(/^a/)) {
        fruits.push(v);
    }
});
alert(fruits);

#3


You can use charAt like so :

您可以像这样使用charAt:

var fruit = 'apple, orange, apricot'.split(', ');
  fruit = $.grep(fruit, function(item, index) {
  return item.charAt(0) === 'a';
});
alert(fruit);