效果图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200807095822313.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ppdHlfY2hvdw==,size_16,color_FFFFFF,t_70#pic_center)
function extract(text) {
const start = '{';
const end = '}';
const exp1 = new RegExp(`\\${start}[^{]*\\${end}`, 'g');
const exp2 = new RegExp(`(?<=\\${start}).*(?=\\${end})`, 'g');
const matchList = text.match(exp1);
if (!matchList) {
return [{
index: 0,
text: text,
type: 0
}];
}
const splitList = text.split(exp1);
let index = 0;
matchList.forEach(function (o, i) {
splitList.splice(i + 1 + index, 0, o);
index++;
});
const filterList = splitList.filter(function (o) {
return o;
});
return filterList.map(function (o, i) {
const m = o.match(exp2);
if (m) {
return {
index: i,
text: m[0],
type: 1
};
} else {
return {
index: i,
text: o,
type: 0
};
}
});
}