开发者

What regular expression can validate this type of string?

开发者 https://www.devze.com 2023-04-07 07:24 出处:网络
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.

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:

  1. First Name
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消