What regular expression can validate this type of string?
How do I validate this using a regex in js?
first.last.wrt
OR
开发者_Python百科first.last
Both are part of email addresses, only the name part in the format mentioned above should validated.
Ex: These should validate using the regex:
john.doe.wrt
john.doe
/^[a-z]+\.[a-z]+(\.wrt)?$/
Based on your example, this would work.
If you only want letters/numbers/underscores to validate:
(\w+?)\.(\w+?)(?:\.wrt)?
If you want anything to validate in the "first" and "last" parts:
(.+?)\.(.+?)(?:\.wrt)?
Capture Groups:
- First Name
- Last Name
if(/^[a-z]+\.[a-z]+(\.wrt)?$/i.test('the string')) {
// It validates!
} else {
// It doesn't...
}
I take it apostrophes and such aren't allowed, but you can add them within the square brackets.
精彩评论