ECMASript 9新特性
ES9语法新特性
对象展开
Rest 参数与spread扩展运算符在 ES6中已经引入,不过 ES6中只针对于数组,在 ES9中为对象提供了像数组一样的 rest参数和扩展运算符
对象的合并
// function connect({ host, port, ...user }) {
// console.log(host);
// console.log(port);
// console.log(user);
// }
// connect({
// host: '127.0.0.1',
// port: 3306,
// username: 'root',
// password: 'root',
// type:'master'
// })
const skillOne = {
a: "西游记"
}
const skillTwo = {
b: "红楼梦"
}
const skillThree = {
c: "水浒传"
}
const skillFour = {
d: "三国演义"
}
const obj ={...skillOne,...skillTwo,...skillThree,...skillFour}
console.log(obj)
正则扩展-命名捕获分组
// //声明一个字符串
// let str = '<a href="https://www.cnblogs.com/wzx-blog/">苏槿年</a>';
// //提取 url 与 标签文本
// const reg = /<a href="(.*)">(.*)<\/a>/;
// //执行
// const result = reg.exec(str);
// console.log(result[1]);
// console.log(result[2]);
let str = '<a href="https://www.cnblogs.com/wzx-blog/">苏槿年</a>';
const reg = /<a href="(?<url>.*)">(?<text>.*)<\/a>/;
const result = reg.exec(str);
// console.log(result);
console.log(result.groups.url);
console.log(result.groups.text);
正则扩展-反向断言
//声明字符串
let str = 'JS51234你知道么555是大小周';
//正向断言(从前往后判断,到“是”)
// const reg = /\d+(?=是)/;
// const result = reg.exec(str);
//反向断言(从后往前判断,到“么”)
const reg = /(?<=么)\d+/;
const result = reg.exec(str);
console.log(result);
正则扩展-dotAll模式
let str =
`<ul>
<li>
<a>肖生克的救赎</a>
<p>上映日期:1994-09-10</p></li>
<li>
<a>阿甘正传</a>
<p>上映日期:1994-07-06</p></li>
</ul>`;
//声明正则
// const reg = /<li>\s+<a>(.*?)<\/a>\s+<p>(.*?)<\/p>/;
//dotAll模式,增加模式修正符“S”
// const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/s;
//全局遍历
const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/gs;
// const result = reg.exec(str);
let result;
let data = [];
while(result = reg.exec(str)){
console.log(result);
data.push({title:result[1],time:result[2]});
}
console.log(data);
// console.log(result)