Skip to content

Commit c5f2abf

Browse files
committed
增加JS正则表达式
1 parent 101e685 commit c5f2abf

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

readme.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5628,21 +5628,162 @@
56285628
### 使用 cookie
56295629

56305630
- 设置 `cookie`
5631+
56315632
- 格式:`名字 = 值`
5633+
56325634
- 赋值时不会覆盖
5635+
56335636
- 过期时间:`expires = 时间`
5637+
56345638
- 设置前一个 cookie 数据的过期时间,expires 放在数据后面:
56355639
- `document.cookie = 'password=123;expires='+ oDate;`
56365640
- 日期对象的使用
5641+
56375642
- 封装函数
5643+
5644+
```js
5645+
// 封装 setCookie 函数
5646+
function setCookie(name, value, iTime) {
5647+
var oDate = new Date();
5648+
oDate.setTime(oDate.getTime() + iTime); // 毫秒
5649+
document.cookie = name + '=' + value + ';expires=' + oDate;
5650+
}
5651+
setCookie('username', '张三', 1000*60*60);
5652+
```
5653+
5654+
5655+
56385656
- 读取 `cookie`
5657+
56395658
- 字符串分割
5659+
5660+
```js
5661+
// 封装 getCookie 函数
5662+
getCookie(name) {
5663+
var arr = document.cookie.split('; ');
5664+
for (var i in arr) {
5665+
var arr2 = arr[i].split('=');
5666+
if (arr2[0] === name){
5667+
return arr2[1];
5668+
}
5669+
}
5670+
return '';
5671+
}
5672+
console.log(getCookie('username'));
5673+
```
5674+
5675+
5676+
56405677
- 删除 `cookie`
5678+
56415679
- 已经过期
56425680

5681+
```js
5682+
// 封装 removeCookie 函数
5683+
function removeCookie(name) {
5684+
setCookie(name, '', -1)
5685+
}
5686+
removeCookie('username');
5687+
```
5688+
5689+
5690+
56435691
- 例子:使用 `cookie` 记录上次登陆的用户名
5692+
56445693
- 提交时:记录用户名
5694+
56455695
- `window.onload`:读取用户名
56465696

5697+
- 代码:
5698+
5699+
```HTML
5700+
<!DOCTYPE html>
5701+
<html>
5702+
<head>
5703+
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
5704+
<title>28.1.登陆后cookie读取用户名</title>
5705+
<style>
5706+
</style>
5707+
<script>
5708+
// 封装 setCookie 函数
5709+
function setCookie(name, value, iTime) {
5710+
var oDate = new Date();
5711+
oDate.setTime(oDate.getTime() + iTime*1000*60*60);
5712+
document.cookie = name + '=' + value + ';expires=' + oDate;
5713+
}
5714+
// 封装 getCookie 函数
5715+
function getCookie(name) {
5716+
var arr = document.cookie.split('; ');
5717+
for (var i in arr) {
5718+
var arr2 = arr[i].split('=');
5719+
if (arr2[0] === name){
5720+
return arr2[1];
5721+
}
5722+
}
5723+
return '';
5724+
}
5725+
// 封装 removeCookie 函数
5726+
function removeCookie(name) {
5727+
setCookie(name, '', -1)
5728+
}
5729+
5730+
window.onload = function () {
5731+
var oUser = document.getElementById('username');
5732+
var oPasswd = document.getElementById('passwd');
5733+
var oBtn_sub = document.getElementById('btn_sub');
5734+
if (getCookie('username')) {
5735+
oUser.value = getCookie('username');
5736+
}
5737+
oBtn_sub.onclick = function () {
5738+
setCookie("username", oUser.value, 1);
5739+
setCookie("passwd", oPasswd.value, 1);
5740+
}
5741+
}
5742+
</script>
5743+
</head>
5744+
<body>
5745+
<form action="28.1.登陆后cookie读取用户名.html" method="GET">
5746+
<input type="text" name="username" id="username">
5747+
<input type="text" name="passwd" id="passwd">
5748+
<input type="submit" name="" id="btn_sub" value="登录">
5749+
</form>
5750+
</body>
5751+
</html>
5752+
```
5753+
5754+
5755+
56475756
## JS 中的正则表达式
56485757

5758+
### 正则表达式基础
5759+
5760+
- 复习字符串操作
5761+
- search:查找
5762+
- substring:获取子字符串
5763+
- charAt:获取某个字符串
5764+
- split:分割某个字符串
5765+
- 找出字符串中所有的数字
5766+
- 用传统字符串操作完成
5767+
- 用正则表达式完成
5768+
5769+
### 字符串与正则配合
5770+
5771+
- search:字符串搜索
5772+
- 返回出现的位置
5773+
- 忽略大小写:i:ignore
5774+
- 判断浏览器类型
5775+
- match:获取匹配的项目
5776+
- 量词:+
5777+
- 量词变化:\d \d\d 和 \d+
5778+
- 全局匹配:g:global
5779+
- 例子:找出所有数字
5780+
- replace:替换所有匹配
5781+
- 返回替换后的字符串
5782+
- 例子:敏感词过滤
5783+
5784+
### 字符串
5785+
5786+
- 任意字符
5787+
- [abc]
5788+
- 例子:
5789+

0 commit comments

Comments
 (0)