js常用方法demo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>网页标题</title> <meta name="keywords" content="关键字列表" /> <meta name="description" content="网页描述" /> <link rel="stylesheet" type="text/css" href="" /> <style type="text/css"></style> <script type="text/javascript">
//大写和小写方法 var str="javascript"; var str1="HTML"; document.write(str.toUpperCase()+"<br>"); document.write(str1.toLowerCase()); document.write("<hr>");
//分割后的数组 var week="星期一|星期二"; var arr=week.split("|"); document.write(arr); document.write("<hr>");
//正则匹配 var str="hello世界,hello美女,hello帅哥"; //创建一个正则表达式 var reg=/hello/g; document.write(str.replace(reg,"你好")); document.write("<hr>"); //重新排序 var arr=["北京市","重庆市","福建市"]; document.write(arr.sort(sortby)); function sortby(c,d){ return c.localeCompare(d); //localeCompare()方法实现中文排序、sort方法实现数字英文混合排序 } </script> </head> <body> </body> </html>
检验字符串是否含特殊符号demo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>网页标题</title> <meta name="keywords" content="关键字列表" /> <meta name="description" content="网页描述" /> <link rel="stylesheet" type="text/css" href="" /> <style type="text/css"></style> <script type="text/javascript">
//检验的字符串,自行修改 var username="qiu<qiongben"; var flag=checkUserName(username);
function checkUserName(user){
//特殊符号包含项,自行修改 var arr=['<','>','#','?','!']; var user_length=user.length; var arr_length=arr.length; for(var i=0;i<user_length;i++){ for(var j=0;j<arr_length;j++){ if(user.charAt(i)==arr[j]){ return false; } } } return true; } if(flag){ document.write("合法"); } else{ document.write("有特殊符号"); } </script> </head> <body> </body> </html>
倒序截取字符串demo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>网页标题</title> <meta name="keywords" content="关键字列表" /> <meta name="description" content="网页描述" /> <link rel="stylesheet" type="text/css" href="" /> <style type="text/css"></style> <script type="text/javascript"> var str="javascript"; var len=str.length;
//数据结果(10-1)倒序字符串下标值 for(var i=str.length;i>0;i--){ document.write(str.charAt(i));//charAt将对应下标 输出结果:tpircsava } </script> </head> <body> </body> </html>