1. 对敏感字符的过滤 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'filter.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <mce:script type="text/javascript"><!-- var filter = /一枪爆头/g; // 将受限制的词句组成正则表达式 var said = "他被人一枪爆头了"; // 将接受检查的语句 if( filter.test( said ) ) // 如果被检查语句中存在受限词句 { alert( "该语句中有限制级词语,系统已经过滤!" ); // 显示警告 } else // 否则 { alert( said ); // 输出原话 } // --></mce:script> <body> This is my JSP page. <br> </body> </html> 2. 一些简单的匹配例子: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <mce:script type="text/javascript"><!-- //1. var reg = /.o./g; // 寻找字符o前后接任意字符组成的有三个字符的字符串 var str = "how are you? o,so am i!"; //2. //var str = "this word is OKOKOKOKokokokok!!!"; //给变量赋初值 //var searchStr = /(OK){2}/gi; //分组的正则表达式 //3. //var result = /(red|black)/; //候选正则表达式 //reStr=result.test(str1); //用test方法检查字符串是否存在 //alert(result.test(str2)); //返回的值为bool型,即true或flase,这里返回的是true //4. // var regex=/(?=com)/; //请输入您的域名以com结尾 var result = str.match(reg); var resultStr = ""; for(var i=0;i<result.length;i++) { resultStr += result[i]; } alert("resultStr is " + resultStr); // --></mce:script> <body> This is my JSP page. <br> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '3.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <mce:script type="text/javascript"><!-- function check() { var str = document.form1.username.value; var reg = /[a-z0-9]/w/d/g; if(reg.test(str)) { alert("匹配成功!"); } else { alert("匹配不成功!"); } } // --></mce:script> <body> <form action="" name="form1"> <center> <p> 用户名合法性检测程序 </p> <p> 规则:数字或英文字符串+数字 </p> <input type="text" name="username" value=""></input> <input type="button" value="匹配" onclick="check()"/> </center> </form> </body> </html>