#vue config 配置
1 | const path = require("path"); |
#vue config 配置
1 | const path = require("path"); |
1 | <template> |
1 | /** |
1 | /** |
1 | import axios from "axios"; |
1 | abbc abbbc |
1 | ab1c ab2c |
1 | [1234][1 - 4]; |
[^ab] 除 ab 以外的任意字符
\d [0-9] 数字
\D [^0-9] 除数字以外的字符
\w [a-z0-9A-Z_] 数字、大小写字母、下划线
\W 非单词字符
\s 空格
\S 非空格
. 任意字符
{m,} 至少出现 m
{m} 出现 m
? {0,1} 出现或者不出现
\d{2,5} 数字 2-5
123 12345 123
\d{2,5}? 虽然 2,5 但是有一次就行了
12, 34, 56
惰性匹配只需要给后面加个?
a|b|c
abc
good|at 如果匹配 goodat 会匹配到 good,可见也是惰性的,匹配一次,后面就不再匹配
匹配颜色
#af0
#000000
/#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/g
匹配时间
23:21
05:59
7:9
/^([01][09]|[2][0-3]):[0-5][0-9]/
/^(0?[0-9]|1[0-9]|[2][0-3]):(0?[0-9]|[0-5][0-9])/
匹配日期
1995-03-29
/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/g
window 操作路径
F:\study\javascript\regex\regular expression.pdf
F:\study\javascript\regex
F:\study\javascript
F:\
代表不能为空的文件名 _出现 0 或者任意次{0,} ?可有可无{0,1}
/^[a-zA-F]:\([^\:<>|”?\r\n]+\)([^\:<>|”?\r\n]+)?$/
匹配id
/id=”.*?”/
/id=”(.?)”/
id=”[^”]“
/^|$/g
(?=p) 表示p前面的位置
“hello”.replace(/(?=l)/g,”#”)
“he#l#lo”
(?!p)
(?!p) 表示非p前面的位置
“hello”.replace(/(?!p)/g,”#”)
#h#ell#o
1 | "hello" = "" + "" + "hello" |
12,345,678
/(?!^)(?=(\d{3})+$)/g
12345678 1234556 12,345,678 123,345
/(?!\b)(?=(\d{3})+\b)/g
\b 表示单词和空格之间的距离
不是\b前的位置 (?!\b) \B
/\B(?=(\d{3})+\b)/g
1800 $ 1,800.00
1 | const format = (num) =>{ |
/(?=.*[0-9])^[a-zA-Z0-9]{6,12}$/ 是否包含数字0-9
/(?=.[0-9])(?=.[a-z])^[a-zA-Z0-9]{6,12}$/ 包含两种
/(?=.*[0-9])^/
表示开头前还有个位置,有任意多个字符,且有数字,简言之字符包含数字
?!p 不包含数字 不包含小写字母 也就是不能单独的存在
/(?!^[0-9]{6,12}$)(?!^[a-z]{6,12}$)^[a-zA-Z0-9]{6,12}$/
(ab)+
(a|b|c)
用括号包裹表示【存储到的分组数据】
/(\d{3})-(\d{2})/
【提取数据】
string.match(reg)
reg.exec(string)
$1-$9
RegExp.$1 构造函数的全局属性
‘2021-03-29’.replace(/(\d{3})-(\d{2})-(\d{2})/,$2/$3/$1)
03/29/2021
2017-06-12”
2017/06/12”
2017.06.12”
2016-06/12”
/\d{4}(-|/|.)\d{2}\1{d}2/
\1 表示前面(-|/|.)匹配到的字符,表示前后两者一致
12345
(\d)+ 5
(?:p)
1 | let str = 'I love js java node' |
1 | 一个是替换 另一个是提取 |
1 | /(?:^|\s)\w/g $1.toUpperCase |
1 | -moz-magic |
MozTransform
-moz-transform
1 | /([a-zA-Z])/g, '-$1' |
1 | <>div< |
// 贪婪12345 \d{1,3} 123 \d{1,3} 45
/“.“/ /“[^”]“/减少回溯
// 惰性 ? 尽可能少
// 分支 a|b|c
/^((0{0,2}\d|0?\d{2}|1\d{2}|2[0-4]\d|25[0-5]).){3}(0{0,2}\d|0?\d{2}|1\d{2}|2[0-4]\d|25[0-5])$/
// 简化
((…).){3} 三位数.三位数.
(0{0,2}\d|0?\d{2}|1\d{2}|2[0-4]\d|25[0-5])
0{0,2}\d 匹配一位数,包括补齐0。 9 09 009 分别0、1 、2三次
0?\d{2} 匹配两位数,包括补齐0。00-99 000-099
1\d{2} 100-199
2[0-4]\d 200-249
25[0-5] 250-255
// 提取公共部分
/this|that/ /th(?is|at)/
// 减少分支数量
/red|read/ /rea?d/
var regex = /^(\d{4})\D(\d{2})\D(\d{2})$/;
var string = “2017-06-26”;
console.log( string.match(regex) );
// =>[“2017-06-26”, “2017”, “06”, “26”, index: 0, input: “2017-06-26”]
1 | // 虚拟 DOM 结构 |
1 | // 虚拟Dom,添加到真实节点 |
1 | console.log( |
1 | Element.prototype.render = function () { |
1 | document.body.appendChild( |
1 | if (_.isString(oldNode) && _.isString(newNode)) { |
1 | if (oldNode.tagName === newNode.tagName && oldNode.key === newNode.key) { |
1 | function patch(node, patches) { |
1 | 20个请求最多5个 |
1 | window.WKwebviewBridge = { |
1 | bridge.callHandler('Name',data,(data)=>{ |
// 上传之前将文件处理为对象
readFiles(files) {
if (!files || files.length <= 0) {
return;
}
for (const file of files) {
const url = window.URL.createObjectURL(file);
const obj = {
title: file.name.replace(/(.[^.]*$)|[_]/gi, ‘’), // 去掉文件后缀
url,
file,
fileType: file.fileType,
status: 0, // 状态 -> 0 等待中,1 完成, 2 正在上传,3 上传失败
percent: 0, // 上传进度
};
// 提前在 data 中定义 list,用来保存需要上传的文件
this.list.push(obj);
}
// 在 data 中定义 startIndex 初始值为 0,上传完成后更新,用于追加上传文件
this.startUpload(this.startIndex);
},
// 上传前需要校验文件
checkFile(index) {
const file = this.list[index];
// 如果文件不存在,即全部文件上传完成
if (!file) {
this.uploadFinished = true; // 上传完成,向父组件抛出 success 事件
this.$emit(‘success’, this.list);
// 清空上传控件中的值,保证 change 事件能正常触发
this.$refs.input.value = null; this.startIndex = index > 1 ? index - 1 : 0;
return false;
}
// 校验是否已上传
if (${file.status}
=== “1”) {
this.startUpload(++index);
return false;
}
// 校验文件大小
if (this.maxSize && file.file && file.file.size >= this.maxSize) {
this.startUpload(++index);
return false;
}
return true;
},// 上传单个文件
startUpload(index) {
if (!this.checkFile(index)) { return; }
// 开始上传,维护上传状态
this.uploadFinished = false;
const file = this.list[index].file;
const fileObj = this.list[index];
// 创建 formData 用于提交
const data = new FormData();
data.append(‘userfile’, file);
axios({
url: this.url, // 上传接口,由 props 传入
method: ‘post’,
data,
withCredentials: true,
cancelToken: this.source.token, // 用于取消接口请求
// 进度条
onUploadProgress: e => {
if (fileObj.status === 1) { return; } // 已上传
// 限定最大值为 99%
const p = parseInt((e.loaded / e.total) * 99);
if (e.total) {
fileObj.status = 2; // 正在上传
fileObj.percent = p; // 更新上传进度
} else {
fileObj.status = 3; // 上传失败
}
},
})
.then(response => {
if (${response.code}
=== “200”) {
fileObj.status = 1;
fileObj.percent = 100;
} else {
fileObj.status = 3;
}
})
.catch(e => {
fileObj.status = 3;
})
.finally(e => {
this.startUpload(++index);
});
// 上传完成
},
data: () => ({
list: [], // 已选择的文件对象
uploadFinished: true, // 上传状态
startIndex: 0, // 开始上传的下标,用于追加文件
source: axios.CancelToken.source(), // axios 取消请求
}),
reset() { // 重置
this.list = [];
this.source.cancel();
this.startIndex = 0;
this.uploadFinished = true;
this.$refs.input && (this.$refs.input.value = null);
},
、拖拽上传
bindEvents() {
const dropbox = this.$refs.pickerArea;
// 防止重复绑定事件,需要在 data 中初始化 bindDrop 为 false
if (!dropbox || this.bindDrop) { return }
// 绑定拖拽事件,在组件销毁时解绑
dropbox.addEventListener(“drop”, this.handleDrop, false);
dropbox.addEventListener(“dragleave”, this.handleDragLeave);
dropbox.addEventListener(“dragover”, this.handleDragOver);
this.bindDrop = true;
},
// 拖拽到上传区域
handleDragOver(e) {
e.stopPropagation();
e.preventDefault();
this.dragging = true;
},
// 离开上传区域
handleDragLeave(e) {
e.stopPropagation();
e.preventDefault();
this.dragging = false;
},
// 拖拽结束
handleDrop(e) {
e.stopPropagation();
e.preventDefault();
this.dragging = false;
const files = e.dataTransfer.files; // 调用
this.$refs.uploadBtn && this.$refs.uploadBtn.readFiles(files);
},
beforeDestroy() {
// 组件销毁前解绑拖拽事件
try {
const dropbox = this.$refs.pickerArea;
dropbox.removeEventListener(“drop”, this.handleDrop);
dropbox.removeEventListener(“dragleave”, this.handleDragLeave);
dropbox.removeEventListener(“dragover”, this.handleDragOver);
this.bindDrop = false;
} catch (e) {}
},