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.
我有一个用于文件名验证的正则表达式。这里是:
/^[0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\(\)\.\%\+\~\_; ]+$/
如何更改它以检查文件名不以.符号开头。感谢帮助。
.
负前瞻将是最简单的解决方案:
/^(?!\.)[0-9a-zA-Z^&'@{}[\],$=!\-#().%+~_; ]+$/
或者,您可以将第一个字符与额外的字符类匹配:
/^[0-9a-zA-Z^&'@{}[\],$=!\-#()%+~_; ][0-9a-zA-Z^&'@{}[\],$=!\-#().%+~_; ]*$/ ^^ no dot here
顺便说一句,在字符类中,几乎所有特殊字符都失去了它们的功能,不需要转义。