正则表达式仅匹配字母数字和空格

ayg*_*eta 4 regex jquery character

嘿伙计们,我对正则表达方式并不擅长.
我不想允许任何其他字符,但字母空格和数字.当然,用户只能输入字母或仅输入数字或字母和数字,而不能输入其他字符.他也可以在字符串之间放置_例子: Hello_World123 这可能是字符串.任何人都可以帮助并为我建立一个正则表达式谢谢.

Tim*_*ker 10

要确保字符串仅包含(ASCII)字母数字字符,下划线和空格,请使用

^[\w ]+$
Run Code Online (Sandbox Code Playgroud)

说明:

^       # 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
Run Code Online (Sandbox Code Playgroud)


Guf*_*ffa 5

简单的这个:

^[\w ]+$
Run Code Online (Sandbox Code Playgroud)

解释:

^ 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
Run Code Online (Sandbox Code Playgroud)