简体   繁体   中英

Regular expression: everything except Latin letters

1)How to say in regular expression: increase match counter only if there are no letters at all?

I came up with this:

"/^[^a-zA-Z]+$/"

and it seems to work, but I don't get why "/^[^a-zA-Z]+/" doesn't work while "/[^a-zA-Z]+$/" works?

2)What does this mean?: "/[a-zA-Z]+/" I thought it means that match counter will increase only if all the elements will be in range az or AZ. But testing shows I'm wrong. Also tried this "/^[a-zA-Z][a-zA-Z]+/" but this also give 1 for "aa11".

Thanks in advance

The only correct regular expression you've posted is /^[^a-zA-Z]+$/ . All the rest are wrong.

You need the ^ and $ to anchor the match to the start and end of the string respectively.

  • /^[^a-zA-Z]+/ matches aaa111 because there's no end-of-string anchor.
  • /[^a-zA-Z]+$/ matches 111aaa because there's no start-of-string anchor.
  • /[a-zA-Z]+/ matches 111aaa111 because there's no start- or end-of-string anchor. It matches if there's any letter anywhere in the string.

I personally like to just use /^[:alpha:]+$/ you start regex with a dilimeter which can be anything so #^[:alpha:]+$# also works. ^ is the start of the input and $ is the end. + is for a match of 1 or more. tutorial/info

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