检查字符串 Javascript 中字符 'x' 是否出现在字符 'y' 之前?

Check if character 'x' comes before character 'y' in a string Javascript?

我正在为我的一个项目制作一个电子邮件验证系统,并且一切正常,直到我遇到错误,如果电子邮件遵循以下结构:

username.domain@domain-ext

那么它将 return 正确。 那么有没有办法检查“@”是否出现在“。”之前?在香草 javascript?

代码如下:

    function validate ( element ) {
      if( element.indexOf ( '@' ) > -1 && element.indexOf ( '.' ) > -1 ){
        //checks if there is anything between '@' and '.'
        var domain_name = element.substring(
          element.lastIndexOf("@") + 1,
          element.lastIndexOf(".")
        );
        if (domain_name != '')
        {
          //checks if there is anything before the '@'
          var username = element.split('@')[0];
          if (username != '') {
            //checks if there is anything after the '.'
            var emailsplit = element.split('.');
            var domain = emailsplit[emailsplit.length - 1];
            if (domain != '')
            {
              return true;
            }
            else {
              return false;
            }
          }
          else {
            return false;
          }
        }
        else {
          return false;
        }
      }
      else {
        return false;
      }
    }
    console.log('username@domain.com : ' + validate('username@domain.com'));
    console.log('@domain.com : ' + validate('@domain.com'));
    console.log('username@.com : ' + validate('username@.com'));
    console.log('username@domain. : ' + validate('username@domain.'));
    console.log('usernamedomaincom : ' + validate('usernamedomaincom'));
    console.log('username.domain@com : ' + validate('username.domain@com'));

我只需要使用 lastIndexOf 添加 1 个 if 语句。谢谢@jabaa

    function validate ( element ) {
      if( element.indexOf ( '@' ) > -1 && element.indexOf ( '.' ) > -1 ){
        if ( element.indexOf('@') < element.lastIndexOf('.') )
        {
          //checks if there is anything between '@' and '.'
          var domain_name = element.substring(
            element.lastIndexOf("@") + 1,
            element.lastIndexOf(".")
          );
          if (domain_name != '')
          {
            //checks if there is anything before the '@'
            var username = element.split('@')[0];
            if (username != '') {
              //checks if there is anything after the '.'
              var emailsplit = element.split('.');
              var domain = emailsplit[emailsplit.length - 1];
              if (domain != '')
              {
                return true;
              }
              else {
                return false;
              }
            }
            else {
              return false;
            }
          }
          else {
            return false;
          }
        }
        else {
          return false
        }
      }
      else {
        return false;
      }
    }
    console.log('username@domain.com : ' + validate('username@domain.com'));
    console.log('@domain.com : ' + validate('@domain.com'));
    console.log('username@.com : ' + validate('username@.com'));
    console.log('username@domain. : ' + validate('username@domain.'));
    console.log('usernamedomaincom : ' + validate('usernamedomaincom'));
    console.log('username.domain@com : ' + validate('username.domain@com'));