问题描述
我想允许使用字母数字字符和句点;但是,该短语不能连续包含两个或更多个句点,它不能以句点开头或结尾,并且不允许使用空格。
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 allowed.
我正在使用两个PHP和Javascript。
I am using both PHP and Javascript.
到目前为止,我有 / ^(?!。* \。{2})[a-zA-Z0- 9。] + $ /
这适用于允许使用字母数字字符和句点,同时拒绝空格和连续句点,但我仍然不确定如何检查开始和/或结束时段。我怎么能这样做?并且,有没有更好的方法来做我已经拥有的东西?
This works for allowing alphanumeric characters and periods, while denying spaces and consecutive periods, but I still am not sure how to check for starting and/or ending periods. How might I do this? and, is there an even better way to do what I already have?
推荐答案
这是一种可能的方式
/^(?!\.)((?:[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
()
这篇关于表达的开头或结尾没有句号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!