這是我的代碼:
const string = `1
00:01:46,356 --> 00:01:49,893
test test test test
2
00:01:50,794 --> 00:01:54,998
test test test red test test
3
00:01:55,199 --> 00:01:58,267
test test red`;
const match = st.split('\n\n').find((e) => e.includes('red'));
console.log(match);
問題是它只回傳第一次出現(第二句中的“紅色”一詞),而最后一句中還有另一個“紅色”。我應該怎么做才能獲得所有事件?
uj5u.com熱心網友回復:
使用查找:
回傳提供的陣列中滿足提供的測驗函式的第一個元素的值
您可以使用過濾器代替查找:
const match = string.split('\n\n').filter((e) => e.includes('red'));
const string = `1
00:01:46,356 --> 00:01:49,893
test test test test
2
00:01:50,794 --> 00:01:54,998
test test test red test test
3
00:01:55,199 --> 00:01:58,267
test test red`;
const match = string.split('\n\n').filter((e) => e.includes('red'));
console.log(match);
uj5u.com熱心網友回復:
只需使用filter代替find.
const st = `1
00:01:46,356 --> 00:01:49,893
test test test test
2
00:01:50,794 --> 00:01:54,998
test test test red test test
3
00:01:55,199 --> 00:01:58,267
test test red`;
const match = st.split('\n\n').filter((e) => e.includes('red'));
console.log(match);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369580.html
標籤:javascript 细绳
上一篇:在GBQ中用星號替換第n個白色
