与正则表达混淆

时间:2014-06-20 13:56:57

标签: regex

我正在以下REGEX工作

/^[a-z][a-z0-9_.-]*$/

一切正常,正如它所示,

abc._-
abc_02.
ab.2_cder

问题 - 它不允许我输入如下所示的[._-]字符。我是REGEX的新手和初学者。

.any character
_any character
-any character

3 个答案:

答案 0 :(得分:3)

 /^[a-z][a-z0-9_.-]*$/

使用^[a-z],您强制将字符串的第一个字符设为小写字母。

所以它不会与其他任何东西一起使用。

您可以将正则表达式更改为

 /^[a-z0-9_.-]*$/

让它发挥作用。

答案 1 :(得分:1)

以下是您的正则表达式的含义:

  • ^:从...开始
  • [a-z]:...一个小写字母......
  • [a-z0-9_.-]*:...在a-z / _ / . / - ...
  • 中添加0个或多个字符
  • $:...并到达行尾

enter image description here

如果您想在a-z / _ / . / -中添加一个或多个字符,请删除第一部分:

/^[a-z0-9_.-]+$/

enter image description here

答案 2 :(得分:0)

模式/^[a-z][a-z0-9_.-]*$/描述:

^             represents the beginning of a line
[a-z]         lowercase letter in the beginning (exactly single time)
[a-z0-9_.-]*  lowercase letters, digits, underscore, dot or hyphen (any one zero or more times) 
$             represents the ending of a line

正则表达式的图示:

enter image description here

你的正则表达式模式强制它在开头用一个小写字母启动它。

注意:空格未添加到您的正则表达式模式中,并且不会与多个单词匹配。

相关问题