用于验证输入的正则表达式正则表达式:两个单词之间有空格 [英] Regular Expression regex to validate input: Two words with a space between
问题描述
我需要使用正则表达式来验证一个带有 php 的字段,该字段必须有两个由空格分隔的单词,例如:First Last",但我找不到适合我的目的,有人可以帮助我吗?
i need to use a regular expression to validate a field with php that has to have two words separated by a space like: "First Last" but i cant find one that fits my purposes, can anyone help me?
我做过的最好的是 ^[a-zA-Z0-9_\s]*$
但有了这个我可以有不止一个空间和领域的任何地方,我只想要字之间.有人可以帮我吗?
The best i've done is ^[a-zA-Z0-9_\s]*$
but with this i can have more than one space and anywhere in the field and i want only between the words. Can anyone help me?
推荐答案
^\w+\s\w+$
之类的东西应该适用于这种情况.但是您不一定需要为此使用正则表达式,您可以使用 explode()
.
Something like ^\w+\s\w+$
ought to work for this case. But you don't necessarily need to use regular expressions for this, you could just use explode()
.
这篇关于用于验证输入的正则表达式正则表达式:两个单词之间有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!