检查字符串中是否包含数组值 [英] Check if array value is included in string

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

问题描述

我正在为某种形式的联系方式进行一些客户端验证,该网站目前不在线,因此服务器端与此无关.

I'm working on some client side validation for a contact form of sorts, the website currently isn't online so server side isn't relevant.

我正在尝试创建一个单词过滤器",以在表单提交"之前赶上任何亵渎性语言.

I am trying to create a 'word filter' to catch on any abusive of obscene language before the form is 'submitted'.

这里有代码,没有s亵内容...

Heres the code, without the obscenities...

function filterInput(str) {
    var inputFilter = ['word1', 'word2', 'word3'];
    var arrayLength = inputFilter.length;

if (inputFilter.indexOf(str) > - 1) {
    // Word caught...
} else {
    // Clear...
}

如果用户输入"word1",它将捕获该单词.如果用户输入"word1word2"或"John is a word3",则不会捕获该单词.

If the user were to enter 'word1', it will catch the word. If the user enters 'word1word2' or 'John is a word3', it doesn't catch it.

本来我有一个for循环,效果更好,但是如果没有单词('word1word2')之间的空格,那还是行不通的.

I originally had a for loop which worked better, but still wouldn't work without whitespace between words('word1word2').

任何输入将不胜感激,我一直在搜索,但没有什么完全符合我的需求.

Any input would be greatly appreciated, I've been searching but nothing quite matches my needs.

所以我也想出了一个解决方案,但是看到可以实现这一目标的各种方式,我很好奇它的工作原理以及为什么特定方法更好?

So I too have come up with a solution, but seeing the varying ways this can be achieved I am curious as to how it works and also why a particular way is better?

这是我想出的...

function filterInput(str) {
    var inputFilter = ['word1', 'word2', 'word3'];
    var arrayLength = inputFilter.length;

    for (var i = 0; i < arrayLength; i++) {
        if (str.includes(inputFilter[i])) {
            window.alert('Message...');
            return;
        } 
    }
}

推荐答案

您正在寻找

You're looking for some rather than indexOf, since you have to do custom matching:

if (inputFilter.some(function(word) { return str.indexOf(word) != -1; })) {
    // Word caught...
} else {
    // Clear...
}

或带有ES2015 +箭头功能和 String.prototype.includes :

Or with an ES2015+ arrow function and String.prototype.includes:

if (inputFilter.some(word => str.includes(word))) {
    // Word caught...
} else {
    // Clear...
}

some 反复调用回调,直到第一次返回真实值.如果回调曾经返回真实值,则 some 返回 true ;否则, some 返回 false .例如,它询问某些"条目是否与谓词功能匹配.( any 可能是一个更好的术语,但是当添加到内置组件中时,TC39委员会必须做很多工作来避免与库之类的冲突.)

some calls the callback repeatedly until the first time it returns a truthy value. If the callback ever returns a truthy value, some returns true; otherwise, some returns false. E.g., it's asking if "some" of the entries match the predicate function. (any may have been a better term, but when adding to the built-ins, the TC39 committee have to do a lot of work to avoid conflicts with libraries and such.)

如果您需要找回实际条目,请使用 findIndex .

If you ever need to get back the actual entry, use find which returns the entry or undefined if not found. If you need its index, use findIndex.

旁注:请注意,做到这一点非常复杂.提防 Scunthorpe问题,当然人们会常规地混淆字母或替代字母的顺序星号或类似的失败过滤器...

Side note: Just beware that it's notoriously complicated to do this well. Beware of the Scunthorpe problem, and of course people will routinely just confuse the sequence of letters or substitute asterisks or similar to defeat filters of this sort...

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

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