3 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果您修復(fù) HTML 以便它可以被解析為 jQuery 對(duì)象,我在下面的示例中通過(guò)添加缺少的前導(dǎo)標(biāo)記來(lái)完成,那么<label>您可以find()定位i元素,然后map()構(gòu)建其文本數(shù)組. 嘗試這個(gè):
let str = '<label>Edges</label><div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(226, 84, 153);"></div><div class="report_color_container"><i>Carnival Pink: </i>7</div></div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(103, 66, 48);"></div><div class="report_color_container"><i>Chocolate Brown: </i>5</div></div></div></div><div><hr><label>';
var colours = $(str).find('i').map((i, el) => el.textContent.trim()).get();
console.log(colours);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
我建議使用正則表達(dá)式。
例如 :
const str = 'Youre string'
const reg = /<i>[\w\s]+/g // find i tag and get all letters and spaces after that
const strings = str.match(reg).map(v => v.slice(3)) // after that remove i tag from resulting array of strings
將輸出 => ["Carnival Pink", "Chocolate Brown"]

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
你可以用正則表達(dá)式來(lái)做到這一點(diǎn)
const STR = 'Edges</label><div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(226, 84, 153);"></div><div class="report_color_container"><i>Carnival Pink: </i>7</div></div><div class="edges_and_corners_container"><div class="color-cont"><img class="color-img" src="img/tiles/light.png" style="background-color: rgb(103, 66, 48);"></div><div class="report_color_container"><i>Chocolate Brown: </i>5</div></div></div></div><div><hr><label>'
const res = STR.match(/(?<=<i>)[^:]*/g)
console.log(res)
添加回答
舉報(bào)