简体   繁体   中英

Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number)

I'm currently using this regex ( /^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/ ) to accept letters, numbers, spaces and underscores. I want to change it like this that it takes the combination of number and the character but not only the number.

If i understand correctly you want to allow a string that begins with at least one letter and optionally is followed by number or underscore or space.

Try this: /^(?:[A-Za-z]+)(?:[A-Za-z0-9 _]*)$/ at this online regex tester .

This should work.

Cheers!

Try this:

/^[A-Za-z0-9 _]*[A-Za-z]+[A-Za-z0-9 _]*$/

This allows for 0 or more of each of the outside groups, and 1 or more of the inner group (letters only). A string of only digits will fail.

Try this:
^(?![0-9]*$)[a-zA-Z0-9\\s_]+$

This expression has a negative lookahead to verify that the string is not only numbers. See it in action with regexr

/^[\w\s]+$/

\\w allows letters, numbers and underscores

\\s allow spaces

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