How to create regex to test for a number in an expression?

问题内容:

I have an email regex that looks like this:

var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;

How can I update this regex to make at least 1 digit, at least 1 letter and a point (.) required?

bert@gmail.com INVALID 
111@22 INVALID 
craig11@gmail.com VALID

问题评论:

1  
Any own research?
    
Also, WHY would you want that?

答案:

答案1:

You could always use regex to match e.g allowed words ([a-zAZ]) and digits (\d) to match e-mail adresses with at least one digit and one character as such:

([a-zA-Z]+\d.+)

which only matches craig11@gmail.com because it fulfils the criteria.

Or to go further, use word boundaries to demand @ and ., such as:

([a-zA-Z]+\d+\b@.+\b.+)

Test it out yourself on http://rubular.com/.

答案评论:

原文地址:

https://stackoverflow.com/questions/47750444/how-to-create-regex-to-test-for-a-number-in-an-expression

添加评论

友情链接:蝴蝶教程