Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有任何正则表达式将字符串的第一个字符匹配为 Underscore( _) 或字母表[a-zA-Z] 第二个字符向前的字符串可以包含 [0-9a-zA-Z] 或只有 2 个特殊字符是 ' _' 和 '-'
_
^[_a-zA-Z][\w-]+$
这是一个带有_, 和字母字符的字符类。 \w是字母数字加号_,因此现金包含在字符类中。 +意思是“一个或多个”。
\w
+
^[_a-zA-Z][\w-]*$
这应该检查 _、az 和 AZ 的第一个字符,然后是字母数字、_ 和“-”。
编辑:用字母数字替换字母
Edit2:删除了不必要的转义字符