使用Regex从数组中删除元素

时间:2022-01-10 21:21:04

I'm using regex to test certain elements in an array of arrays. If an inner array doesn't follow the desired format, I'd like to remove it from the main/outer array. The regex I'm using is working correctly. I am not sure why it isn't removing - can anyone advise or offer any edits to resolve this problem?

我正在使用regex测试数组中的某些元素。如果内部数组不符合所需的格式,我希望将它从主/外部数组中删除。我正在使用的regex正在正常工作。我不知道为什么它没有被删除——有人能建议或提供任何编辑来解决这个问题吗?

for (var i = arr.length-1; i>0; i--) {

      var a = /^\w+$/;
      var b = /^\w+$/;
      var c = /^\w+$/;  

    var first = a.test(arr[i][0]);
    var second = b.test(arr[i][1]);
    var third = c.test(arr[i][2]);

if ((!first) || (!second) || (!third)){
arr.splice(i,1);
}

2 个解决方案

#1


4  

When you cast splice method on an array, its length is updated immediately. Thus, in future iterations, you will probably jump over some of its members.

在数组上强制转换splice方法时,它的长度会立即更新。因此,在未来的迭代中,您可能会跳过它的一些成员。

For example:

例如:

var arr = ['a','b','c','d','e','f','g']

for(var i = 0; i < arr.length; i++) {
  console.log(i, arr)
  if(i%2 === 0) {
    arr.splice(i, 1) // remove elements with even index
  }
}

console.log(arr)

It will output:

它将输出:

0 ["a", "b", "c", "d", "e", "f", "g"]
1 ["b", "c", "d", "e", "f", "g"]
2 ["b", "c", "d", "e", "f", "g"]
3 ["b", "c", "e", "f", "g"]
4 ["b", "c", "e", "f", "g"]

["b", "c", "e", "f"]

My suggestion is, do not modify the array itself if you still have to iterate through it. Use another variable to save it.

我的建议是,如果还需要迭代数组,不要修改数组本身。使用另一个变量来保存它。

var arr = ['a','b','c','d','e','f','g']
var another = []

for(var i = 0; i < arr.length; i++) {
  if(i%2) {
    another.push(arr[i]) // store elements with odd index
  }
}

console.log(another) // ["b", "d", "f"]

Or you could go with Array.prototype.filter, which is much simpler:

或者你可以用array。prototype。过滤器,更简单:

arr.filter(function(el, i) {
  return i%2 // store elements with odd index
})

It also outputs:

它还输出:

["b", "d", "f"]

#2


1  

Your code seems to work to me. The code in your post was missing a } to close the for statement but that should have caused the script to fail to parse and not even run at all.

你的代码似乎对我有用。您的文章中的代码缺少一个}来关闭for语句,但这应该导致脚本无法解析,甚至根本无法运行。

I do agree with Leo that it would probably be cleaner to rewrite it using Array.prototype.filter though.

我同意Leo的观点,使用Array.prototype重写它可能会更简洁。过滤器。

The code in your question would look something like this as a filter:

您的问题中的代码看起来像这样作为一个过滤器:

arr = arr.filter(function (row) {
  return /^\w+$/.test(row[0]) && /^\w+$/.test(row[1]) && /^\w+$/.test(row[2]);
});

jsFiddle

jsFiddle

I'm assuming it is 3 different regular expressions in your actual code, if they are all identical in your code you can save a little overhead by defining the RegExp literal once:

我假设在你的实际代码中有3个不同的正则表达式,如果它们在你的代码中都是相同的,你可以通过定义RegExp文字来节省一点开销:

arr = arr.filter(function (row) {
  var rxIsWord = /^\w+$/;
  return rxIsWord.test(row[0]) && rxIsWord.test(row[1]) && rxIsWord.test(row[2]);
});

#1


4  

When you cast splice method on an array, its length is updated immediately. Thus, in future iterations, you will probably jump over some of its members.

在数组上强制转换splice方法时,它的长度会立即更新。因此,在未来的迭代中,您可能会跳过它的一些成员。

For example:

例如:

var arr = ['a','b','c','d','e','f','g']

for(var i = 0; i < arr.length; i++) {
  console.log(i, arr)
  if(i%2 === 0) {
    arr.splice(i, 1) // remove elements with even index
  }
}

console.log(arr)

It will output:

它将输出:

0 ["a", "b", "c", "d", "e", "f", "g"]
1 ["b", "c", "d", "e", "f", "g"]
2 ["b", "c", "d", "e", "f", "g"]
3 ["b", "c", "e", "f", "g"]
4 ["b", "c", "e", "f", "g"]

["b", "c", "e", "f"]

My suggestion is, do not modify the array itself if you still have to iterate through it. Use another variable to save it.

我的建议是,如果还需要迭代数组,不要修改数组本身。使用另一个变量来保存它。

var arr = ['a','b','c','d','e','f','g']
var another = []

for(var i = 0; i < arr.length; i++) {
  if(i%2) {
    another.push(arr[i]) // store elements with odd index
  }
}

console.log(another) // ["b", "d", "f"]

Or you could go with Array.prototype.filter, which is much simpler:

或者你可以用array。prototype。过滤器,更简单:

arr.filter(function(el, i) {
  return i%2 // store elements with odd index
})

It also outputs:

它还输出:

["b", "d", "f"]

#2


1  

Your code seems to work to me. The code in your post was missing a } to close the for statement but that should have caused the script to fail to parse and not even run at all.

你的代码似乎对我有用。您的文章中的代码缺少一个}来关闭for语句,但这应该导致脚本无法解析,甚至根本无法运行。

I do agree with Leo that it would probably be cleaner to rewrite it using Array.prototype.filter though.

我同意Leo的观点,使用Array.prototype重写它可能会更简洁。过滤器。

The code in your question would look something like this as a filter:

您的问题中的代码看起来像这样作为一个过滤器:

arr = arr.filter(function (row) {
  return /^\w+$/.test(row[0]) && /^\w+$/.test(row[1]) && /^\w+$/.test(row[2]);
});

jsFiddle

jsFiddle

I'm assuming it is 3 different regular expressions in your actual code, if they are all identical in your code you can save a little overhead by defining the RegExp literal once:

我假设在你的实际代码中有3个不同的正则表达式,如果它们在你的代码中都是相同的,你可以通过定义RegExp文字来节省一点开销:

arr = arr.filter(function (row) {
  var rxIsWord = /^\w+$/;
  return rxIsWord.test(row[0]) && rxIsWord.test(row[1]) && rxIsWord.test(row[2]);
});