mozaic
mozaic

Reputation: 21

Regex must have letters only and one dot

I am trying to build a regex that must contain letters only and one dot.

There is a restriction: the dot cannot be placed in beginning or the end.

Example:

abc.difference

I tried to use

^[a-zA-z]*(\d*\.\d*)[a-zA-z]*$

Upvotes: 1

Views: 1207

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626853

f the dot can go missing in the input string, you can use

/^[A-Za-z]+(?:\.[A-Za-z]+)?$/

If the dot is obligatory:

/^[A-Za-z]+\.[A-Za-z]+$/

Details:

  • ^ - start of string
  • [A-Za-z]+ - one or more letter chars
  • (?:\.[A-Za-z]+)? - an optional occurrence of
    • \. - a dot
    • [A-Za-z]+ - one or more alpha chars
  • $ - end of string.

Upvotes: 2

Related Questions