简体   繁体   English

一个只允许字母、数字、破折号和点的验证正则表达式看起来如何,强制至少一个点而不是第一个字符?

[英]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感谢WiktorPeter引导我做到这一点

$ 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.

相关问题 JavaScript Regex - 如何检查字符串是否仅包含字母、数字、破折号、下划线和点 - JavaScript Regex - How to check if a string only contains letters, numbers, dashes, underscores and dots 如何在 javascript 中创建仅允许字母、数字、破折号 -、下划线 _ 和空格的 function? - How do I create a function in javascript that only allows letters, numbers, dashes -, underscores _ and spaces? 正则表达式仅允许使用字母,数字,点,下划线和破折号。 至少5个字符 - regex allow only letters, numbers, dot, underscore, dash. at least 5 characters 正则表达式,只允许在名字/姓氏验证中使用一个空格 - Regex that allows only one space in the first/last name validation 允许字母数字字符且仅允许单个“-”的正则表达式 - Regex which allows alphanumeric character and only single "-" 正则表达式,开头只能有一个+号,然后是数字 - Regex that allows only one + sign at the beginning and then numbers RegExp第一个字符不能是点,只能是数字 - RegExp first character cannot be dot and only numbers 允许使用一个或多个连续大写字母和数字的 PascalCased 的正则表达式 - Regex that allows PascalCased with one or more consecutive uppercase letters and numbers 如何创建一个包含数字、字母或一个点的正则表达式 - How to create a regex which will include numbers, letter or one dot 使用 Javascript 验证至少 8 个字符和仅字母和数字的组合 - Input validation of at least 8 characters and only combinations of letters and numbers using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM