動漫人物
2019-02-26 21:59:24
JS如何利用正則優(yōu)雅地提取markdown中的圖片(URL)?
2 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個贊
不是很清楚你的具體需求,直接提取不就是了
const str = "......";
const result = str.match(/!\[(.*?)\]\((.*?)\)/);
console.log(result); // ["", "a", "b"]
獲取多個的話可以用exec
const str = ".........";
const pattern = /!\[(.*?)\]\((.*?)\)/mg;
const result = [];
let matcher;
while ((matcher = pattern.exec(str)) !== null) {
result.push({
alt: matcher[1],
url: matcher[2]
});
}
console.log(result); // [{ alt: 'a', url: 'b' }, { alt: 'c', url: 'd' }]
添加回答
舉報
0/150
提交
取消