Skip to content Skip to sidebar Skip to footer

No Periods At Beginning Or End Of Expression?

I want to allow alphanumeric characters and periods; however, the phrase cannot contain more two or more periods in a row, it cannot start or end with a period, and spaces are not

Solution 1:

It nearly always helps to draw a finite state machine to conceptualize what your regular expression should look like.

^(?:\w\.?)*\w$

Solution 2:

here's a possible way

/^(?!\.)((?:[a-z\d]|(?<!\.)\.)+)(?<!\.)$/i

for more explanations and tests see here: http://www.regex101.com/r/rZ6yH4

edit: according to tyler's solution, here's him way, shortened and reduced to letters and digits

/^(?:[a-z\d]+(?:\.(?!$))?)+$/i

( http://www.regex101.com/r/dL5aG0 )

Solution 3:

A start would be:

/^[^. ](?!.*\.{2})[a-zA-Z0-9.]+[^. ]$/

but it should be tested carefully.

Post a Comment for "No Periods At Beginning Or End Of Expression?"