ES9新特性
1、扩展运算符与rest参数
rest参数
Rest 参数与 spread 扩展运算符在 ES6 中已经引入,不过 ES6 中只针对于数组,在 ES9 中为对象提供了像数组一样的 rest 参数和扩展运算符。
function connet({host,port,...user}){
console.log(host);
console.log(port);
console.log(user);//多余的参数全部都在user里
}
//调用
connet({
host:"127.0.0.1",
port:3333,
username:"张三",
password:123
})
扩展运算符
const skillOne={
q:"技能1",
w:"技能2"
}
const skillTwo={
e:"技能3",
r:"技能4"
}
const mangseng={...skillOne, ...skillTwo}
console.log(mangseng);
2、正则表达式
2.2、正则表达式命名捕获组
ES9 允许命名捕获组使用符号?,这样获取捕获结果可读性更强
let str = '尚硅谷';
const reg = /(?.*)<\/a>/;
const result = reg.exec(str);
console.log(result.groups.url);
console.log(result.groups.text)
2.3、正则表达式反向断言
ES9 支持反向断言,通过对匹配结果前面的内容进行判断,对匹配进行筛选。
//声明字符串
let str = 'JS5211314 你知道么 555 啦啦啦';
//正向断言
const reg = /\d+(?=啦)/;
const result = reg.exec(str);
//反向断言
const reg = /(?<=么)\d+/;
const result = reg.exec(str);
console.log(result);
2.4、正则表达式 dotAll 模式
正则表达式中点.匹配除回车外的任何单字符,标记s改变这种行为,允许行
终止符出现
let str = `
`;
//声明正则
const reg = /.*?(.*?)<\/a>.*?(.*?)<\/p>/gs;
//执行匹配
const result = reg.exec(str);
let result;
let data = [];
while(result = reg.exec(str)){
data.push({title: result[1], time: result[2]});
}
//输出结果
console.log(data);