我是regex和typescript的新手,正在尝试编写一个正则表达式来匹配以下内容:
1. must start with alphanumeric (repeating is fine)
2. can contain alphanumeric (repeating is fine)
3. can contain periods, underscores, and/or hyphens (no repeating)
4. must end with alphanumeric (repeating is fine)
我是一个有帮助的助手,可以为您翻译文本。
我一直在搜索,找到了许多类似的例子,并尝试将它们调整为我需要的工作,但我还没有能够找到正确的解决方案。如果有人有一个好的正则表达式来帮助并解释为什么这样做,那就太棒了,因为我可以学习更多关于系统的知识。
以下是我正在尝试验证为可接受字符串的一些示例:
this.is.Valid
also_a_valid_1
Me-too.im_an-ugly.but_vALid-5tring
我的当前正则表达式允许一些无效的字符串,但应该失败,因为它具有重复的句点/连字符/下划线,并且在开头和结尾处有句点、连字符、下划线,以下是一些例子:
-this..should..not.be.valid....
..THIS__.-also-should..fail-
why..IS_regex--so.confusing-for-n0obs
这里是我正在使用的正则表达式示例:
validateString(myString: string): boolean {
return (/^[a-zA-Z0-9_\-\.]+((\.-?|-\.?)[a-zA-Z0-9_\-\.]+)*$/.test(varKey))
}