确定一个字符串是否包含3个字数组中每个字的3个字 [英] Determine if a string contains 3 words in order from each of 3 word arrays

查看:226
本文介绍了确定一个字符串是否包含3个字数组中每个字的3个字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从三个单词数组中按顺序测试一个字符串以查看它是否包含单个单词?当测试字符串消息(见下文)按顺序包含单词时,我想在控制台中打印 true 来自三个阵列。顺序中的第一个单词应该是 first_word 数组中的任何单词。第二个单词应该是 second_word 数组中的任何单词,而对于 third_word 数组中的第三个单词则相同。单词将始终由单个空格分隔,而不是其他分隔符。

How can I test a string to see if it contains a single word each, in order, from three arrays of words? I want true to be printed in the console when the test string message (see below) contains words, in order, from the three arrays. The first word in order should be any of the words in the first_word array. The second word should be any word in the second_word array, and the same for the third word from the third_word array. The words will always be separated by a single space, and no other separator character(s).

应在测试字符串中的任何位置检测单词(即可以存在在单词之前或之后的任何内容)。如果 first_word second_word中的元素之前或之后存在某些内容,它仍应打印 true third_word

The words should be detected anywhere within the test string (i.e. there can be any contents prior to, or after the words). It should still print true if there is something before or after the elements from first_word, second_word, and third_word.

这就是我目前所拥有的:

This is what I currently have:

var message = '';
var first_word  = ['nice',  'cool', 'awesome'];
var second_word = ['red',   'blue', 'green'];
var third_word  = ['apple', 'pear', 'mango'];

if (message.includes(first_word + ' ' + second_word + ' ' + third_word)) {
   console.log(true);
}

应该发生的测试用例:

message = 'nice blue mango'                  // true
message = 'red nice apple'                   // false (not in order)
message = 'red nice apple, nice green apple' // true (contains words in order somewhere in string)
message = 'nice red'                         // false (does not contain third word)
message = 'nice very blue mango'             // false (there is a word in-between)

第三个例子是真的,因为不错green apple是正确顺序的数组元素。

The third example is true because nice green apple are elements from the array in the right order.

推荐答案

使用正则表达式



在我看来,执行此操作的简单方法是生成正则表达式(RegExp)。然后使用该RegExp来测试你想要的单词序列。

Use a regular expression generated from your word arrays

In my opinion, the easy way to do this is to generate a regular expression (RegExp) from your word arrays. Then use that RegExp to test for the sequence of words that you desire.

//Create the RegExp
regExp = new RegExp([first_word, second_word, third_word].map(list => '(?:' + list.join('|') + ')').join(' '));
//Check the test cases
testCases.forEach(message => console.log(regExp.test(message)));

<!-- Setup from question -->
<script>
    let testCases = [
        'nice blue mango',                  // true
        'red nice apple',                   // false (not in order)
        'red nice apple, nice green apple', // true
        'nice red',                         // false (does not contain third word)
        'nice very blue mango',             // false (there is a word in-between)
        'lol nice blue mango',              // true (additional, from OP's comment)
    ];

    var first_word  = ['nice',  'cool', 'awesome'];
    var second_word = ['red',   'blue', 'green'];
    var third_word  = ['apple', 'pear', 'mango'];
</script>

打破这一点,这样做首先创建一个包含我们正在搜索的单词数组的数组:

Breaking this down, what this does is first create an array containing the arrays of words for which we are searching:

[first_word, second_word, third_word]

结果:

[
    ["nice","cool","awesome"],
    ["red","blue","green"],
    ["apple","pear","mango"]
]

.map() 然后调用该数组将该数组映射到每组单词的正则表达式文本数组中。 .join(' |') 用于将列表中的所有单词连接成一个字符串,其中的单词由 | 分隔。

[first_word, second_word, third_word].map(list => '(?:' + list.join('|') + ')')

结果:

[
    "(?:nice|cool|awesome)",
    "(?:red|blue|green)",
    "(?:apple|pear|mango)"
]

然后 .join('') 编辑成一个字符串,每个列表用空格分隔。这导致:

These are then .join(' ')ed into one string with spaces separating each list. This results in:

(?:nice|cool|awesome) (?:red|blue|green) (?:apple|pear|mango)

这是用于创建常规字符串的字符串表达式,使用 new正则表达式() 。然后使用正则表达式 .test() 每个消息字符串,以查看指定的单词是否按所需顺序排列。

which is the string used to create the regular expression, using new RegExp(). The regular expression is then used to .test() each message string to see if the specified words are in the desired order.

虽然简单,但使用RegExp可能不是获得结果。如果您正在测试巨大的数量的字符串,那么可能希望通过使用替代方法来改善性能。

While easy, using a RegExp may not be the highest performance method for obtaining the result. If you're testing huge numbers of strings, then you might want to look into improving performance by using an alternate method.

这篇关于确定一个字符串是否包含3个字数组中每个字的3个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆