开发者

Regular expression for checking a string, which must contain a space and be alpha numeric

Can any one give the regular expression for checkin开发者_高级运维g a string

which must contain a space and be alpha numeric.


In JavaScript you can solve it like this:

if (/^[a-z\d]* [a-z\d]*$/i.test(input)) {
    // Successful match
} else {
    // Fail
}

Notes:

  • The i following the regex means ignore case, i.e. it matchez a-z as well as A-Z
  • The regex says: in the beginning, find zero to many alphanums, then a space and finally zero to many alphanums before the string ends
  • the string you test is input
  • If you want to make sure that there's at least one alphanum before and after the space, then use + instead of *


I'm assuming you want the space to be between two alpha numeric substrings. I have also added \s* to allow for whitespace encasing the alpha-numerics (which judging by your comment is what you actually want)

^\s*[0-9a-zA-Z]+[ ][0-9a-zA-z]+\s*$


/^[A-Za-z0-9]+ [A-Za-z0-9]+$/

If the space can be at the end or the beginning, change the correspondent + to *.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜