tempestor
tempestor

Reputation: 51

Regex string doesn't contain 2 dots in a row

I'd like to know if this regex expression is correct for checking that a string doesn't start with a dot, doesn't end with a dot and contains at least one dot anywhere but not the start or end:

My issue is that I can't figure on how to check if there's 2 dots in a row.

/^([^.])+([.])+.*([^.])$/

Upvotes: 5

Views: 1979

Answers (2)

Toto
Toto

Reputation: 91430

You're close, have a try with:

^[^.]+(?:\.[^.]+){2,}$

It maches strings that have 2 or more dot, but not at the begining or at the end.

If you want one or more dot:

^[^.]+(?:\.[^.]+)+$

If you want one or two dots:

^[^.]+(?:\.[^.]+){1,2}$

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

It seems you need to use

^[^.]+(?:\.[^.]+)+$

See the regex demo

Details:

  • ^ - start of string
  • [^.]+ - 1+ chars other than a . (so, the first char cannot be .)
  • (?:\.[^.]+)+ - 1 or more (thus, the dot inside a string is obligatory to appear at least once) sequences of:
    • \. - a dot
    • [^.]+ - 1+ chars other than . (the + quantifier makes a char other than . appear at least once after a dot, thus, making it impossible to match the string with 2 dots on end)
  • $ - end of string.

Upvotes: 4

Related Questions