admin管理员组文章数量:1291455
How can I write a regular expression in javascript that only allows users to write this:
abc.def
, abc-def
or abc
So basically match a pattern that only contains letters (only lowercase [a-z]) and a .
or -
. But does not match -
or .
at the beginning or end of string or multiple times(only one .
or -
per string)
So not allowing them to do:
.....
abc...abc
abc.abc....
abc----....
...abc.abc
.abc
-abc
etc.
How can I write a regular expression in javascript that only allows users to write this:
abc.def
, abc-def
or abc
So basically match a pattern that only contains letters (only lowercase [a-z]) and a .
or -
. But does not match -
or .
at the beginning or end of string or multiple times(only one .
or -
per string)
So not allowing them to do:
.....
abc...abc
abc.abc....
abc----....
...abc.abc
.abc
-abc
etc.
- Are dot or hyphen allowed as the first character of the string? – John Watts Commented Jun 24, 2012 at 23:08
- I forgot to add that. No, a dot or hyphen is not allowed at the beginning of the string. – georgesamper Commented Jun 24, 2012 at 23:13
-
May be this -
/^[a-z]+(?:[-.]?[a-z]+)?$/
- is what you're looking for? ) Single dash or dot in ALL the string. – raina77ow Commented Jun 24, 2012 at 23:18
1 Answer
Reset to default 10Regex would be: /^[a-z]+([\.\-]?[a-z]+)?$/
JavaScript:
var text = 'abc.def';
var pattern = /^[a-z]+([\.\-]?[a-z]+)?$/;
if (text.match(pattern)) {
print("YES!");
} else {
print("NO!");
}
See and test the code here.
本文标签: javascriptRegEx only one dot inside string not at beginning or endStack Overflow
版权声明:本文标题:javascript - RegEx only one dot inside string not at beginning or end - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741531898a2383804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论