[英]How does a validation regex look like which allows letters, numbers, dashes and dots only, forces at least one dot but not as first character?
I am really bad at Regex but in my JavaScript app I am trying to validate a form item, the value of the form item can only contain letters, numbers, dashes and at least one dot.我真的不擅长正则表达式,但在我的 JavaScript 应用程序中,我正在尝试验证表单项,表单项的值只能包含字母、数字、破折号和至少一个点。
So the following would be valid:所以以下是有效的:
hello.world
microsoft.com
So far I came up with this (.+)[a-zA-Z0-9.-]\.[a-zA-Z0-9.-]+$
but it doesn't work as I can add spaces.到目前为止,我想出了这个
(.+)[a-zA-Z0-9.-]\.[a-zA-Z0-9.-]+$
但它不起作用,因为我可以添加空格。 What can I do to make my Regex work?我可以做些什么来使我的正则表达式工作? Also can I prevent the dot being the first character?
我也可以防止点成为第一个字符吗?
I think you can achieve this with the following我认为您可以通过以下方式实现这一目标
^[^.][a-zA-Z0-9.-]+$
^[^.]
is no starting dots ^[^.]
没有起始点
[a-zA-Z0-9.-]
is all characters lowercase, uppercase and digits including dot and hyphens. [a-zA-Z0-9.-]
是所有字符小写、大写和数字,包括点和连字符。 Credit to Wiktor & Peter for leading me to this感谢Wiktor和Peter引导我做到这一点
$
makes sure it's the end of the string, so it won't match on a partial like "hello world" $
确保它是字符串的结尾,所以它不会像“hello world”这样的部分匹配
Example here: https://regex101.com/r/BXHmwh/1此处示例: https://regex101.com/r/BXHmwh/1
The so far accepted answer does not fully meet the OP's requirements which are...迄今为止接受的答案并不完全符合 OP 的要求,这些要求是......
the value of the form item can only contain letters , numbers , dashes and at least one dot .
表单项的值只能包含字母、数字、破折号和至少一个点。 ... Also... prevent the dot being the first character ...
...另外...防止点成为第一个字符...
NJDawson 's regex... ^[^.][a-zA-Z0-9.-]+$
... does fail at the last requirement because it allows as first character any character which is not a dot where the OP requires any of the above quoted characters except the dot. NJDawson的正则表达式...
^[^.][a-zA-Z0-9.-]+$
... 在最后一个要求中确实失败了,因为它允许作为第一个字符的任何字符不是 OP 所在的点需要除点之外的任何上述引用字符。
Example:例子:
const regX = (/^[^.][a-zA-Z0-9.-]+$/); console.log( "regX.test('.foo-bar.baz-9')?", // false as expected regX.test('.foo-bar.baz-9') // because of dot first. ); console.log( "regX.test('#foo-bar.baz-9')?", // true which violates regX.test('#foo-bar.baz-9') // the requirements ); // due to using #.
One can fully meet the requirements by updating the regex to something more expressive like... ^[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]*$
... which makes sure that neither a dot nor any other character than one of the allowed ones sits at the first position of the to be tested string and also forces the string to contain at least one dot in addition to the other allowed characters.通过将正则表达式更新为更具表现力的内容,可以完全满足要求...
^[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]*$
... 这使得确保除了允许的字符之一之外,点或任何其他字符都不位于要测试的字符串的第一个 position 处,并且除了其他允许的字符外,还强制字符串至少包含一个点。
Updated example:更新示例:
const regX = (/^[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]*$/); console.log( "regX.test('.foo-bar.baz-9')?", // false as expected regX.test('.foo-bar.baz-9') // because of dot first. ); console.log( "regX.test('#foo-bar.baz-9')?", // false as expected regX.test('#foo-bar.baz-9') // because of hash first. ); console.log( "regX.test('+foo-bar.baz-9')?", // false as expected regX.test('+foo-bar.baz-9') // because of plus first. ); console.log( "regX.test('-foo-bar.baz-9')?", // true as expected. regX.test('-foo-bar.baz-9') ); console.log( "regX.test('9.-foo-bar.baz')?", // true as expected. regX.test('9.-foo-bar.baz') ); console.log( "regX.test('9a-foo-bar-baz')?", // false as expected. regX.test('9a-foo-bar-baz') // bacause of missing dot. );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.