jQuery检查电子邮件地址是数组元素中字符串的一部分

时间:2016-04-05 12:28:32

标签: javascript jquery

我想检查一个电子邮件地址是否等于3个不同数组中任何字符串的子字符串。不幸的是,我写的函数不断给出“无法识别的表达式”错误。

var search = 'info@example.com'
jQuery.each(array1, array2, array3, jQuery(function(index, value) {
  jQuery.each(value, jQuery(function(key, cell) {
    if (jQuery(search).indexOf(cell) !== -1)
      console.log('Found email in ' + index, cell)
  }))
}))

这会引发以下错误:

Uncaught Error: Syntax error, unrecognized expression: info\\@example\\.com

我错过了什么?

1 个答案:

答案 0 :(得分:5)

首先,每个需要将三个数组包装为单个数组。第二个问题是在匿名函数之前使用jQuery。第三个问题是将search字符串包装在jQuery对象中(这是触发错误的字符串)。

已更新 - demo

jQuery.each([array1, array2, array3], (function(index, value) {
    jQuery.each(value, (function(key, cell) {
        if (search.indexOf(cell) !== -1)
            console.log('Found email in '+index, cell)
    }))
}))