Test if string contains only letters (a-z + é ü ö ê å ø etc..)

后端 未结 12 1331
不思量自难忘°
不思量自难忘° 2020-12-04 18:12

I want to match a string to make sure it contains only letters.

I\'ve got this and it works just fine:

var onlyLetters = /^[a-zA-Z]*$/.test(myString)         


        
12条回答
  •  半阙折子戏
    2020-12-04 18:42

    There should be, but the regex will be localization dependent. Thus, é ü ö ê å ø won't be filtered if you're on a US localization, for example. To ensure your web site does what you want across all localizations, you should explicitly write out the characters in a form similar to what you are already doing.

    The only standard one I am aware of though is \w, which would match all alphanumeric characters. You could do it the "standard" way by running two regex, one to verify \w matches and another to verify that \d (all digits) does not match, which would result in a guaranteed alpha-only string. Again, I'd strongly urge you not to use this technique as there's no guarantee what \w will represent in a given localization, but this does answer your question.

提交回复
热议问题