举例:String strRegex = "^[1][3,4,5,8][0-9]{9}$";校验手机号码
String str = "13977895777"
str.matches(strRegex )
JAVA好像真不能直接和JS的一样,不过我写了其他的办法,就当是从JS转成了JAVA版的吧,呵呵public
class
Test
{
public
static
void
main(String[]
args)
{
System.out.println(getResults("12a4.678"))
}
public
static
int
getResults(String
s){
char
c[]
=
s.toCharArray()
if(s.length()
<
4){
return
0
}
int
ls
=
0
if
(matches(c,
"^[a-z]+$")){
ls++
}
if
(matches(c,
"^[0-9]+$")){
ls++
}
if
(matches(c,
"^[.[^a-z0-9]]+$")){
ls++
}
if
(s.length()
<
6
&&
ls
>
0){
ls--
}
return
ls
}
public
static
boolean
matches(char
c[],
String
regex){
for
(int
i
=
0
i
<
c.length
i++)
{
if(String.valueOf(c[i]).matches(regex)){
return
true
}
}
return
false
}
}