将单词(字符串)数组转换为regex,并使用它获取字符串上的匹配

时间:2021-05-27 15:45:11

What is the most concise and efficient way to translate an array of strings in a regex and then use the regex multiple times on different strings to get the result matches and then iterate over them? Now I'm using the following:

翻译regex中的字符串数组,然后在不同的字符串上多次使用regex以获得结果匹配,然后对它们进行迭代,最简洁、有效的方法是什么?现在我用的是:

var myArray = ['peaches', 'bananas', 'papaya', 'supercity'];
var myString = 'I want some papaya and some peaches';

var regexFromMyArray = new RegExp(myArray.toString().replace(/,/g, '|'), 'gi');

var matches = myString.match(regexFromMyArray) || [];

if (matches.length) {
  for (var i = 0, l = matches.length; i < l; i++) {
    console.log('Found: ' + matches[i]);
  }
}

performance is important here, so plain javascript please.

性能在这里很重要,所以请使用普通的javascript。

1 个解决方案

#1


14  

Just join with pipeline, using Array.join

只需使用Array.join加入管道

var regexFromMyArray = new RegExp(myArray.join("|"), 'gi');

and just do this as if condition is just redundant.

如果条件是冗余的就这么做。

for(var i = 0; i < matches.length; i++)
   console.log("Found:", matches[i]);
  1. A single method is being used instead of initial 3. (toString internally calls join(",")) and replace function is also not used.
  2. 一个方法被使用而不是初始的3。(toString内部调用join(“,”))和替换函数也不被使用。
  3. We have removed an unnecessary if-condition. So that's pretty quick.
  4. 我们已经排除了不必要的假设条件。这是非常快速。

And since you talk about regexes, I'd like to say that

既然你谈到了regex,我想说一下。

  1. A single regex initialization isn't going to cost you much.
  2. 一个单一的regex初始化不会花费太多。
  3. If your objective is really to match the words in the array, then just go with String.indexOf, which is a non-regex form of solving the same.
  4. 如果你的目标是匹配数组中的单词,那么就使用字符串。indexOf,这是一种非正则表达式的解决方法。

#1


14  

Just join with pipeline, using Array.join

只需使用Array.join加入管道

var regexFromMyArray = new RegExp(myArray.join("|"), 'gi');

and just do this as if condition is just redundant.

如果条件是冗余的就这么做。

for(var i = 0; i < matches.length; i++)
   console.log("Found:", matches[i]);
  1. A single method is being used instead of initial 3. (toString internally calls join(",")) and replace function is also not used.
  2. 一个方法被使用而不是初始的3。(toString内部调用join(“,”))和替换函数也不被使用。
  3. We have removed an unnecessary if-condition. So that's pretty quick.
  4. 我们已经排除了不必要的假设条件。这是非常快速。

And since you talk about regexes, I'd like to say that

既然你谈到了regex,我想说一下。

  1. A single regex initialization isn't going to cost you much.
  2. 一个单一的regex初始化不会花费太多。
  3. If your objective is really to match the words in the array, then just go with String.indexOf, which is a non-regex form of solving the same.
  4. 如果你的目标是匹配数组中的单词,那么就使用字符串。indexOf,这是一种非正则表达式的解决方法。