在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  HTML/ JS如何利用正則優(yōu)雅地提取markdown中的圖片?

JS如何利用正則優(yōu)雅地提取markdown中的圖片?

![](URL)

JS如何利用正則優(yōu)雅地提取markdown中的圖片(URL)?

回答
編輯回答
我不懂

不是很清楚你的具體需求,直接提取不就是了

const str = "...![a](b)...";
const result = str.match(/!\[(.*?)\]\((.*?)\)/);

console.log(result); // ["![a](b)", "a", "b"]

獲取多個(gè)的話(huà)可以用exec

const str = "...![a](b)...![c](d)...";
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' }]
2017年1月10日 16:13
編輯回答
深記你

示例:

var regexp = /!\[\]\((.*?)\)/g;
var str='fadfasf![](http://example.com/1.jpg)fasdas![](http://example.com/2.jpg)';
while((result=regexp.exec(str))!==null){console.log(result[1])}

利用正則對(duì)象的exec方法。當(dāng)正則帶有g這個(gè)flag時(shí),每次exec會(huì)更新位置,直到到最后會(huì)返回null

2018年2月6日 14:44