检查字符串是否不包含值

check if string does not contain values

我正在使用 indexOf 查看电子邮件是否包含特定文本以外的任何内容。

例如,我想检查一封电子邮件是否在 @ 符号后不包含“usa”,并显示一条错误消息。

我首先拆分文本并删除@符号之前的所有内容:

var validateemailaddress = regcriteria.email.split('@').pop();

然后,我检查文本是否不包含“usa”:

if(validateemailaddress.indexOf('usa')){
    $('#emailError').show();
} 

上面的检查似乎有些不对。它有效 - 我可以输入电子邮件,如果它不包含 'usa',则会显示错误消息。

无论如何,当我添加额外的检查时,比如如果电子邮件不包含“can”,那么无论如何都会显示错误消息。

如下:

if(validateemailaddress.indexOf('usa') || validateemailaddress.indexOf('can')){
    $('#emailError').show();
} 

如上所述,使用上述方法,无论电子邮件是否包含文本,都会显示错误消息。

我只想检查电子邮件是否包含 'usa' 或 'can',如果不包含,则显示错误消息。

我怎样才能使这个工作?

我相信这个正则表达式匹配正是您要找的

System.out.println(myString.matches("(.)@(.)美国(.*)"));

你可以做类似的事情,使用包括:

const validateEmailAdress = (email) => {
  const splittedEmail = email.split('@').pop();
  
  return (splittedEmail.includes('usa') || splittedEmail.includes('can'))
}

console.log("Includes usa: ", validateEmailAdress("something@gmail.usa"))
console.log("Includes can: ", validateEmailAdress("something@gmail.can"))
console.log("Does not includes: ", validateEmailAdress("something@gmail.com"))

有几种方法可以检查字符串是否 contains/does 不包含子字符串。

String.prototype.includes

'String'.includes(searchString); // returns true/false

String.prototype.indexOf

// returns values from -1 to last postion of string.
'String'.indexOf(searchString); 

// In combination with ~ this can work similar to includes()
// for strings up to 2^31-1 byte length

// returns 0 if string is not found and -pos if found.

~'String'.indexOf(searchString); 

在正则表达式的帮助下:

// substring must be escaped to return valid results
new RegExp(escapedSearchString).test('String'); // returns true/false if the search string is found

'String'.match(escapedSearchString); // returns null or an array if found

总的来说,您可以使用几乎所有方法,例如:

if ('String'.function(searchString)) {
  // 'String' includes search String
} else {
  // 'String' does not include search String
}

或者在 indexOf 的情况下:

if ('String'.indexOf(searchString) > -1) {
  // 'String' includes search String
} else {
  // 'String' does not include search String
}
// OR
if (~'String'.indexOf(searchString)) {
  // 'String' includes search String
} else {
  // 'String' does not include search String
}

这是一个简单的 JavaScript 函数,用于检查电子邮件地址是否包含 'usa' 或 'can'。

function emailValid(email, words) {
    // Get the position of @ [indexOfAt = 3]
    let indexOfAt = email.indexOf('@');

    // Get the string after @ [strAfterAt = domain.usa]
    let strAfterAt = email.substring(indexOfAt + 1);

    for (let index in words) {
        // Check if the string contains one of the words from words array
        if (strAfterAt.includes(words[index])) {
            return true;
        }
    }

    // If the email does not contain any word of the words array
    // it is an invalid email
    return false;
}

let words = ['usa', 'can'];

if (!emailValid('abc@domain.usa', words)) {
    console.log("Invalid Email!");
    // Here you can show the error message
} else {
    console.log("Valid Email!");
}