正则表达式:拉丁字母除外

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 a-z or A-Z. 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