我需要做一个正则表达式,如果字符串中的前两个字符是2个字母,剩下的只有数字,长度为2-10,则返回true。
我无法尝试。我只是不知道该怎么做。
这就是前一个家伙留给我的东西:
function clearTerm($term) {
if( preg_match('/^[a-z0-9()\- ]+$/i', $term) ) {
return true;
} else {
return false;
}
}这是我试过的,但没有希望。我不知道怎么检查前两个,然后再检查其余的。
function clearTerm($term) {
if( preg_match('/^([a-z0-9]+$/i{2})', $term) ) {
return true;
} else {
return false;
}
}regex需要返回true if:前两个字符是两个小写字母(lg),其余是数字,长度为2到10个数字。
所以,lg01234 -> True, lgx1 -> False
试过但失败了,所以这里问。
发布于 2014-05-04 13:20:31
function clearTerm($term) {
if( preg_match('/^[a-z]{2}[0-9]{2,10}$/', $term) ) {
return true;
} else {
return false;
}
}
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[a-z]{2} any character of: 'a' to 'z' (2 times)
--------------------------------------------------------------------------------
[0-9]{2,10} any character of: '0' to '9' (between 2 and 10 times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the stringhttps://stackoverflow.com/questions/23456688
复制相似问题