开发者

Javascript regular expression for one capital

开发者 https://www.devze.com 2023-03-09 02:34 出处:网络
I want to make a regular expression for a string that, to be accepted, needs to have the first letter a capital and the other characters are letters. This is what I tri开发者_运维百科ed:

I want to make a regular expression for a string that, to be accepted, needs to have the first letter a capital and the other characters are letters. This is what I tri开发者_运维百科ed:

^[A-Z*[a-z]

This doesn't work though.


^[A-Z][a-zA-Z]+$

Is probably what you want. If the other letters can't be uppercase, remove the second A-Z.


Yes @minitech is correct. Although this only specifies the beginning of the string (edit: @minitech's answer has now corrected this). It would match "Adf dfgdfg 6756 #%^^%&". To match only strings containing a capital letter followed by other letters use:

^[A-Z][a-zA-Z]+$

This will match "Aa" but not "A". If you want to match "A" then replace the "+" with "*".

0

精彩评论

暂无评论...
验证码 换一张
取 消