Regular expression to match only letter numbers and spaces

Hey guys, I'm not so good at regular expressions.
I do not want to allow any other characters except spaces and numbers of letters. Of course, the user can enter only letters or only numbers or letters and numbers, but not other characters. It can also put _ between examples of strings: <B> Hello_World123 This may be a string. Can someone help and create a regex for me? Thanks.

+4
source share
3 answers

For a string to contain only (ASCII) alphanumeric characters, underscores, and spaces, use

^[\w ]+$ 

Explanation:

 ^ # Anchor the regex at the start of the string [\w ] # Match an alphanumeric character, underscore or space + # one or more times $ # Anchor the regex at the end of the string 
+10
source

This is simple:

 ^[\w ]+$ 

Explanation:

 ^ matches the start of the string \w matches any letter, digit, or _, the same as [0-9A-Za-z_] [\w ] is a set that that matches any character in \w, and space + allows one or more characters $ matches the end of the string 
+4
source

you can use [\ w \ d] +. You can try http://gskinner.com/RegExr/

-3
source

Source: https://habr.com/ru/post/1393597/


All Articles