简体   繁体   English

正则表达式可以使用javascript匹配字符串中任意长度的字符?

[英]Regular expression to match an arbitrary length of characters in a string using javascript?

I m just learning regular expression matching using javascript, and have a doubt regarding the scenario described below. 我只是在学习使用javascript进行正则表达式匹配,并对下面描述的情况有疑问。

I'm trying to validate email, with condition below: 我正在尝试验证电子邮件,但条件如下:

Format should be xxx@yyy.zzz where 格式应为xxx@yyy.zzz ,其中
A) "xxx", "yyy" and "zzz" parts can take values only between lowercase a and z A)“ xxx”,“ yyy”和“ zzz”部分只能取小写字母a和z之间的值
B) The length of zzz, yyy and xxx parts are arbitrary (should be minimum one though) B)zzz,yyy和xxx部分的长度是任意的(尽管应该是最小的)

Now I understand I can build the regex like this: EDIT: CORRECTED REG EXP /[az]+@[az]+\\.[az]+/ 现在,我知道我可以像这样构建正则表达式: 编辑:正确的REG EXP /[az]+@[az]+\\.[az]+/

and the above would evaluate a string like "aaa@aaa.aaa" as true. 上面的代码会将“ aaa@aaa.aaa”之类的字符串评估为true。

But my concern is, in the above expression if i provide "a999@aaa.aaa" , again it would evaluate as true. 但是我担心的是,在上面的表达式中,如果我提供"a999@aaa.aaa" ,它将再次被评估为true。 Now, if i modify the reg ex as **/[az]{1,}@[az]+\\.[az]+/** even then it would evaluate "a999@aaa.aaa" as true because of the presence of "a" as the first character. 现在,如果我将正则表达式修改为**/[az]{1,}@[az]+\\.[az]+/** ,则由于"a999@aaa.aaa" ,它也会将其评估为true存在“ a”作为第一个字符。

So, I would like to know how to match the first part "xxx" in the email "xxx@yyy.zzz" in a way that it checks the entire string from first char till it reaches the @ symbol with the condition that it should take only a to z as valid value. 因此,我想知道如何匹配电子邮件"xxx@yyy.zzz"中的第一部分“ xxx”,以便检查从第一个字符开始的整个字符串,直到到达@符号为止,条件是仅将a到z作为有效值。

In other words, the regex should not mind the length of the username part of email, and irrespective of the number of chars entered, it should test it based on the specified regex condition, and it should test it for the set of chars from index 1 to index of @. 换句话说,正则表达式不介意电子邮件的用户名部分的长度,并且不管输入的字符数如何,都应根据指定的正则表达式条件对其进行测试,并应针对索引中的字符集进行测试1到@的索引。

/[a-z]+@[a-z]\.[a-z]+/

will not match "aaa@aaa.aaa", but will match "aaa@a.aaa". 将不匹配“ aaa@aaa.aaa”,但将匹配“ aaa@a.aaa”。 You are only allowing for a single lowercase letter between the @ and the . 您只允许在@和。之间使用一个小写字母。

Again, the string "a999@aaa.aaa" will not match. 同样,字符串“ a999@aaa.aaa”将不匹配。 [az]+ means look for one or more characters between a and z... digits are not included, so 999 wil not be matched. [az] +表示在a和z之间寻找一个或多个字符...数字不包括在内,因此999将不匹配。

does the regex 正则表达式

/[az]+@[az]+/.[az]+/ /[az]+@[az]+/.[az]+/

do what you want? 你想做什么?

Your regular expression will not show a999@aaa.aaa to be a match. 您的正则表达式不会显示a999@aaa.aaa为匹配项。 It will, however, show that 9999a@aaa.aaa9999 is a match. 但是,它将显示9999a@aaa.aaa9999是一个匹配项。 The regular expression markers you're missing are ^ for the start of string and $ for end of string. 您缺少的正则表达式标记是^ (字符串开头)和$ (字符串结尾)。 This is what you're looking for: 这是您要寻找的:

/^[a-z]{1,}@[a-z]+\.[a-z]+$/

You can test this expression out over on this online validator: http://tools.netshiftmedia.com/regexlibrary/# 您可以在此在线验证器上测试此表达式: http : //tools.netshiftmedia.com/regexlibrary/#

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

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