简体   繁体   English

正则表达式可以在javascript中进行验证

[英]regex to validate in javascript

So I'm trying to validate an email address with the following rules: 1 to 64 characters (lowercase, uppercase, digits or .) plus @ plus another 1 to 64 characters (lowercase, uppercase, digits or .) 因此,我尝试使用以下规则验证电子邮件地址:1到64个字符(小写,大写,数字或。)加上@加另外1到64个字符(小写,大写,数字或。)。

I tried with this 我尝试了这个

function validate(str) {
      const regex = /([a-zA-Z0-9\.]){1,64}$@([a-zA-Z0-9\.]){1,64}$/;
      return regex.test(str);
    }

but it's not working. 但它不起作用。 Any idea why? 知道为什么吗?

You inserted the end of string anchor before the @ symbol and did not use the start of string anchor ^ . 您在@符号之前插入了字符串锚的末尾,但没有使用字符串锚^的开始。 Also, remove the redundant capturing groups. 另外,删除冗余捕获组。

Use 采用

regex = /^[a-zA-Z0-9.]{1,64}@[a-zA-Z0-9.]{1,64}$/
         ^                 ^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM