Reputation:
i built this regexp to match letters and numbers and dots
dots only if they are not repeated successfully
example :
something.somethnElse.another.then.something
this is a match because the dots are separated. but in the following case :
something..thensomething
is no match because there is one or more dots next to eachother
this is my regexp, recommend me please why it's not working
[a-zA-Z0-9\.]+(?!\.{2,})
i tried also
[a-zA-Z0-9\.]+(?![\.]+)
but they both give a match for successful dots
Upvotes: 4
Views: 87
Reputation: 785196
You are close. You can use this regex:
^(?!.*?\.{2})[a-zA-Z0-9.]+$
PS: No need to escape the dot inside character class
Upvotes: 1
Reputation: 70732
You need to use beginning of string ^
and end of string $
anchors and place the lookahead at the beginning.
/^(?!.*\.{2})[a-z0-9.]+$/i
Upvotes: 1
Reputation: 173572
First of all, the expression should be anchored, otherwise it only requires a very minimal match.
Additionally, you could think of your expression as a chain of letters and digits that can be interrupted by exactly one dot.
So:
/^(?:[a-z0-9]+|\.(?!\.))*$/
Upvotes: 1