假設我有一個 html 字串react native。由于沒有documentin react-native,我對如何實作這一點有點困惑
const htmlString = '<div style="margin-left: 10px">I have three dogs. One is called <b>Tiger</b>, the second is called <b>Lion</b>, and the third is called <b>Panda</b>.</div>'
我想提取每個<b>標簽內的文本。這個 html 字串中可以有超過 10 個<b>標簽。<b>標簽也可以嵌套在字串中的許多級別上
預期輸出:
Tiger
Lion
Panda
該資料可以存盤在陣列中或作為物件。請注意:這是在react-native
謝謝
我嘗試了類似的東西
htmlString.split('<b>').pop().split('</b>')[0]
只回傳Panda. 也找不到獲得以前的方法
uj5u.com熱心網友回復:
你可以試試這個:
const reg = /<b>(.*?)<\/b>/g
for (const match of htmlString.matchAll(reg)) {
console.log(`matched: ${match[1]}`);
}
結果:
matched: Tiger
matched: Lion
matched: Panda
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/528293.html
