一、cookie
cookie:
由web服务器创建的,存储在本地计算机中的少容量数据
用户名,密码,id,只能存储4KB
谷歌和欧朋不支持本地存储
如果没有过期时间,一般浏览结束后销毁cookie
作用:方便服务器识别用户,存储账号密码
cookie缺点:容量限制 4KB
由http明文传递,容易被窃用,盗取
cookie每一次请求都会自动添加到请求头Request Headers,增加容量
1.存储cookie
//1.存储:document.cookie="key=value"
document.cookie="userName=ujiuye";
//2.存储多个,需要写多次
document.cookie="passWord=123456";
//3.key同名会覆盖
document.cookie = "passWord=654321";
2.获取cookie
//2.获取cookie
var cookie = document.cookie;
console.log(cookie); //"userName=ujiuye; passWord=123456"
var arr = cookie.split("; ");//分号加空格
console.log(arr);//["userName=ujiuye", "passWord=123456"]
console.log(arr[1].split("=")[1]); //["passWord","123"]
3.设置过期时间
//如果不设置过期时间,默认页面关闭cookie就会被清除
console.log(document.cookie);
//1.设置过期时间 document.cookie="key=value;expires=时间对象"
var oDate = new Date();
oDate.setSeconds(oDate.getSeconds()+10); //10天以后
document.cookie = "userName=123;expires="+oDate; /
封装
存储
//封装一个存储cookie的函数
function setCookie(key,value,day){//var key value,day
var oDate = new Date();
day = day ? day : 7;
oDate.setDate(oDate.getDate() +day);
document.cookie = key+"="+value+";expires="+oDate;
获取
function getCookie(key){
/* var c = document.cookie.split("; ");
["userName=123", "passWord=456" "passWord1=456"]
for(var i = 0;i<c.length;i++){
var arr = c[i].split("="); //[userName,123]
if(arr[0]===key){
return arr[1];
}
} */
var obj = {};
var c = document.cookie.split("; "); // [ "userName=123", "passWord=456" "passWord1=456"]
for(var i = 0;i<c.length;i++){
var arr = c[i].split("="); //userName=123 ["userName","123"]
obj[arr[0]] = arr[1]
}
return obj[key];
}
删除
//删除cookie
function removeCookie(key){
setCookie(key,1,-1); //将要删除的cookie再存储一遍,但是设置时间过期
}
二.正则对象
正则表达式:==是对字符串操作的一种逻辑公式,就是用事先定义好的一些特殊字符,及这些特定字符的组合,组成一个‘规则字符串’,这个规则字符串用来表达对字符串的一种过滤逻辑。==简单的说就是规定文本检索的内容 ,通常被用来检索、替换文本。
1.创建正则对象
//1.创建正则对象(\正则表达式): RegExp
//var reg = new RegExp("检索的内容","修饰符")
var reg = new RegExp("o","ig");
console.log(reg);
//2.字面量创建
var reg = /o/ig;
2.修饰符
g:global 全局匹配
i:ignore case 不区分大小写匹配
var str = "w-e-r-t-y";
var reg1 = /-/;
var reg2 = /-/g;
console.log(str.replace(reg1, "*")); //w*e-r-t-y
console.log(str.replace(reg2, "*")); //w*e*r*t*y
var str = "ui0617UI0617";
var reg3 = /ui/g;
var reg4 = /ui/gi;
console.log(str.replace(reg3, "web")); //web0617UI0617
console.log(str.replace(reg4, "web")); //web0617web0617
3.正则的检索方式
//1.replace
var str = "w2e3r4t5y";
console.log(str.replace(/\d/g, "*"));
//2.split()
console.log(str.split(/\d/));
//3.match 挑选满足条件的组成一个新的数组
console.log(str.match(/\d/g));
//6.search -- 返回被查询字符串首次出现的位置,找不到返回-1 与indexOf用法一样 与indexOf用法一样 indexOf不支持正则 search支持正则
console.log(str.search(/\d/));
//正则提供的方法
//test:检索字符串是否满足条件,满足true 不满足false
//g:全局匹配 默认每次从字符串下标为0的位置开始检索,加了全局匹配,下一次匹配的时候,会从当前匹配到的位置往后
var str = "web123";
var reg = /\d/g;
console.log(reg.test(str)); //true
console.log(reg.test(str)); //true
console.log(reg.test(str)); //true
console.log(reg.test(str)); //false
//exec:具体的检索,返回检索到内容
var str = "web123";
var reg = /\d/g;
console.log(reg.exec(str)); //["1", index: 3, input: "web123", groups: undefined]
console.log(reg.exec(str)); //["2", index: 3, input: "web123", groups: undefined]
console.log(reg.exec(str)); //["3", index: 4, input: "web123", groups: undefined]
console.log(reg.exec(str)); //null
4.元字符-单个字符
//1.---------单个字符--------------------
//. : 匹配除换行符以外的任意字符
var str = "\n2019IT0617web";
var reg = /./g;
console.log(reg.exec(str));
//2019IT0617web
var str = "2019IT0708web";
var reg = /2019IT....web/g;
console.log(reg.exec(str));
//[] : 匹配字符集中的任意一位字符 [0-9a-zA-Z]
var str = "433110220010807011s";
var reg = /433110220010807011[0-9x]/g; //匹配0-9或者x其中的任意一位
console.log(reg.exec(str));
//[^]:匹配除字符集中的任意一位字符
var str = "abcd0123";
var reg = /[^a-z]/;
console.log(reg.exec(str));//0
//\d:匹配数字 \D:匹配非数字
var str = "web0617";
var reg = /\d/;
console.log(reg.exec(str));
var str = "*1w2e3r4t5";
console.log(str.split(/\d/));
console.log(str.split(/\D/));
//\w:匹配数字,字母 _ \W
var str = "%$$r&_fe";
var reg = /\w/;
console.log(reg.exec(str));//r
//\s:匹配空格 \S:匹配非空格
var str = " hello world ";
console.log(str.replace(/\s/g, ""));
//去除首尾空格 " hello world! ";
//\b:匹配字符边界 \B:匹配非字符边界
var str = "not a number,notanumber";
var reg = /\ba\b/g;
console.log(str.replace(reg, "A"));
//^1:以1开头 a$:以a结束
var str = "888888";
var reg = /^\d\d\d\d\d\d$/g;
console.log(reg.exec(str));
5.元字符--多个字符
// ? :匹配前面字符0次或者1次 a? : 匹配a 0次或者1次
var str = "aweb";
var reg = /a?/;
console.log(reg.exec(str));
var str = "window.alert()";
var reg = /(window\.)?alert\(\)/; //转义
console.log(reg.exec(str));
// * : 匹配前面字符0次或多次,尽可能多匹配(连续的) a*:
var str = "aaaawebaaaa";
var reg = /a*/g;
console.log(reg.exec(str));
// + : 匹配前面的字符至少一次,尽可能多匹配
var str = "web";
var reg = /a+/;
console.log(reg.exec(str));
var reg = /http:\/\/www.[a-z0-9A-Z]+/;
//{n,m}:匹配前面字符至少n次,最多m次
var str = "666666";
var reg = /^\d{6}$/;
console.log(reg.exec(str));
var str = "6666666";
var reg = /^\d{6,}$/;
console.log(reg.exec(str));
var str = "3759487655352432423";
var reg = /^[1-9]\d{4,10}$/;
console.log(reg.exec(str));
// | 或
//http https
var str = "http://www.baidu.cn";
var reg = /(http|https):\/\/www\.[a-zA-Z0-9]{2,}\.(com|cn|net|udu|org|cc|gov)/;
console.log(reg.exec(str));
//() 分组 :进行正则匹配的时候,会将()中匹配的到内容,返回
var str = "hello world";
var reg = /(hello) (world)/;
console.log(reg.exec(str));
console.log(RegExp.$1);
console.log(RegExp.$2);
console.log(str.replace(reg, "$2 $1"));
6.其他
//(?:) : 非获取匹配
var str = "web1";
var reg = /web(1|2)/;
console.log(reg.exec(str));
var str = "web1";
var reg = /web(?:1|2)/;
console.log(reg.exec(str));
//(?=) : 正向肯定预查
var str = "web123";
var reg = /[a-z](?=\d)/; //匹配一个字符(后面必须接数字)
console.log(reg.exec(str)); //b
//(?!):正向否定预查
var str = "1w2e34";
var reg = /\d(?![a-zA-Z])/; //匹配一个数字(后面不能接字母)
console.log(reg.exec(str));
//(?<=):反向肯定预查
var str = "web123";
var reg = /(?<=[a-zA-Z])\d/; //匹配一个数字(前面必须是字母)
console.log(reg.exec(str));//1
//(?<!):反向否定预查
var str = "web123";
var reg = /(?<![a-zA-Z])\d/; //匹配一个数字(前面不能是字母)
console.log(reg.exec(str));
var str = "eeee";
var reg = /(?!^\d+$)^[0-9a-zA-Z]+$/; //(?!^\d+$) 不能是纯数字
console.log(reg.exec(str));
//4.正向肯定预查
var str = "weg34"; //获取后面接数字的字母
var reg = /[a-zA-Z](?=\d)/; //字母后面必须有数字
console.log(reg.exec(str));//g
//用户名,字母和数字的组合
//(?=.*[a-zA-Z].*)
var reg = /(?=.*[a-zA-Z].*)(?=.*\d.*)^[a-zA-Z0-9]{3}$/;
var str = "q2e";
console.log(reg.exec(str));
//正向否定预查
var str = "f1d2ae3";//获取后面不接数字的字母
var reg = /[a-zA-Z](?!\d)/; //字母后面不能是数字
console.log(reg.exec(str)); //"a"
//用户名,字母和数字的组合
//(?!^[a-zA-Z]+$) 不能是纯字母
var reg = /(?!^[a-zA-Z]+$)(?!^\d+$)^[a-zA-Z0-9]{3}$/; //不能是纯字母
var str = "1e3";
console.log(reg.exec(str));
var str = "web1021web1021web1021";
var reg = /web/g;
str = str.replace(reg,function(a){
console.log(a); //会把每一次正则匹配到的内容,传递到a里面
return a+"*";
});
console.log(str);//web*1021web*1021web*1021
来源:CSDN
作者:yee只鸟儿.
链接:https://blog.csdn.net/yeee1128/article/details/103408301