<html>
<head>
<script type="text/javascript">
function checkEmail(){
var email = document.getElementById("email").value;
var reg = new RegExp("^[\\w\\d]+@[\\w\\d]+\\.[\\w\\d]+$");
if(reg.test(email)){
alert("it is a email");
}
else{
alert("it is not email");
}
}</script></head>
<body>
<input type="text" id="email"/>
<input type="button" onclick="checkEmail();" value="check"/>
</body>
</html>
<head>
<script type="text/javascript">
function checkEmail(){
var email = document.getElementById("email").value;
var reg = new RegExp("^[\\w\\d]+@[\\w\\d]+\\.[\\w\\d]+$");
if(reg.test(email)){
alert("it is a email");
}
else{
alert("it is not email");
}
}</script></head>
<body>
<input type="text" id="email"/>
<input type="button" onclick="checkEmail();" value="check"/>
</body>
</html>
reg="\\w+";
Pattern p = Pattern.compile(reg);
Matcher m =p.matcher(a);
while(m.find()){
System.out.println(m.group(0));
}
Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("begin java Java JAVA it d odg JAva end");
StringBuffer sb = new StringBuffer();
int i = 0;
while(m.find()){
++i;
if(i%2==0){
m.appendReplacement(sb,"java");
}else{
m.appendReplacement(sb,"JAVA");
}
}
m.appendTail(sb);
System.out.println(sb);
好处就是:对字符串处理起来比较灵活,发便。
if (!jTextField1.getText().matches("^[A-Za-z]+$")) {
JOptionPane.showMessageDialog(null, "对不起,姓名只能是26个英文字母!", "提示",
JOptionPane.ERROR_MESSAGE);
} else if (!jTextField3.getText().matches("\\d{15}|\\d{18}")) {
JOptionPane.showMessageDialog(null, "对不起,中国的身份证为15位或18位", "提示",
JOptionPane.ERROR_MESSAGE);
} else if (!jTextField4.getText().matches("^[A-Za-z]+$")) {
JOptionPane.showMessageDialog(null, "对不起,请用26个英文字母表示地名", "提示",
JOptionPane.ERROR_MESSAGE);