2019-09-05 利用正则优雅地提取markdown中的图片
2021-09-30 本文已影响0人
追寻1989
示例:
getBmobFileList(content){//提取markdown中的图片地址,传入markdown内容
const pattern = /!\[(.*?)\]\((.*?)\)/mg;
const result = [];
let matcher;
while ((matcher = pattern.exec(content)) !== null) {
result.push({
alt: matcher[1],
url: matcher[2]
});
}
console.log(result); // [{ alt: 'a', url: 'b' }, { alt: 'c', url: 'd' }]
},
利用正则对象的exec方法。当正则带有g这个flag时,每次exec会更新位置,直到到最后会返回null。