1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以在那些雙換行符處拆分文本,然后單獨(dú)分析每個(gè)頁(yè)面。我會(huì)這樣做:
let data = `resignations / Friday resignations. adm. mancom .: berenguer llinares
appointments. adm. unique: calvo Friday valenzuela. other concepts: change of the administrative body:
joint administrators to sole administrator. change of registered office. ptda colomer, 6, Friday
Official Gazette of the Commercial Registry
no. 182 Friday, September 18, 2020 p. 33755
cve: borme-a-2020-182-03 verifiable in
sarria). registry data. t 2257, f 100, s 8, h a 54815, i / a 4 (10.09.20) .`
function analyseText(text, wordsToFind) {
const pages = data.split("\n\n");
const result = {};
for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) {
analysePage({
pageIndex,
pageText: pages[pageIndex]
}, wordsToFind, result);
}
return Object.keys(result).map(k => result[k]);
}
function analysePage(page, wordsToFind, result) {
const {
pageText,
pageIndex
} = page;
wordsToFind.forEach(word => {
const count = (pageText.match(new RegExp(word, 'g')) || []).length;
if (count > 0) {
if (!result[word]) {
result[word] = {
name: word,
pageIndices: [],
count: 0
};
}
result[word].pageIndices.push(pageIndex);
result[word].count += count;
}
});
}
const result = analyseText(data, ['resignations', "administrators", "Friday"]);
console.log(result);
在此示例中,我只是打印每一頁(yè)的結(jié)果,但您當(dāng)然可以構(gòu)建一些結(jié)果對(duì)象,在其中保存每一頁(yè)的結(jié)果。
添加回答
舉報(bào)