我有一个页面,管理员可以设置被列入黑名单但不允许在电子邮件地址中使用的单词。在注册页面上,我有一个数组填充了所有这些单词。这个页面是内部的,用于请求创建多个电子邮件地址,因此对于每个请求的电子邮件地址,我需要查看该字词是否存在于数组中。Javascript黑名单阵列中的部分单词
我可以用典型的jQuery inArray
做到这一点,但是我需要弄清楚如何实现部分匹配。
- 实施例
var blackList = Array('admin', 'webform', 'spam');
... Loop over the requested email addresses and put them into an array
var requestedEmails = Array('[email protected]', '[email protected]', '[email protected]');
// Check for the exact match in the arrays
$(requestedEmails).each(function(){
if(jQuery.inArray(this, blackList) != -1) {
// Word is in the array
} else {
// Word is not in the array
}
});
在上面的例子中,我需要它赶上webform1
和admins
既是对那些词存在于以某种方式黑名单。
从这里添加部分匹配的最佳方法是什么?
的可能的复制[如何检查一个字符串是否包含另一个子字符串?](http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring) – Craicerjack
哪个jQuery版本你正在用吗?如果您的版本足够高,请使用grep http://api.jquery.com/jquery.grep/ –
1)'inArray',2)循环,然后与'substr'比较。 3.)使用Levenshtein距离 – st2erw2od