简体   繁体   中英

Simple regex expression to find capital letters is returning small letters

I'm expecting this simple Regex expresison to return null since there are no capital letters in

 var a = "hiho"; var res = a.match(/[AZ]/gi); alert(res) 

the string.

Yet, it is returning an array of each small letter.

Why?

You're using the ignoreCase ( i ) flag, which makes your regex case-insensitive . Remove it and your regex should work as intended.

var res = a.match(/[A-Z]/g);

In the expression

/[A-Z]/gi

The "i" means "case insensitive", which is why both lower and upper case are matching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM