如何从一个段落中找到多个单词匹配?

时间:2018-12-30 17:48:05

标签: javascript loops

有没有一种方法可以匹配段落中的多个单词,同时省略逗号,句号,空格和区分大小写?更好的是,返回答案,前面带有一个升序列表。

我在圣诞节期间收到了此转发的消息,并认为使用代码来解决它会很有趣。

const段落=“我曾经对圣经中的隐藏书籍发表评论。某种程度上的冷漠,使人们对事实持如此艰辛的印象,而对于其他人,则是一种启示。有些人陷入了困境,尤其是自从书名没有大写,但最终,事实真相吸引了我们的读者。对其他人来说,这是一份工作,我们希望这对您来说是最迷人的一刻。是的,会有一些真正容易的事情其他人可能会要求法官帮助找到他们。我会很快承认,通常需要一位部长来找到其中的一位,发现时会大声哀叹。一位小女士说她泡了一杯茶,所以她可以更好地集中精力。看看竞争如何。现在放松,因为在本段中确实有十六本圣经。”

function findBooks() {
  let newParagraph = paragraph.toLowerCase();
  let regEx = /[.,\s]/g;
  let workingPara = newParagraph.replace(regEx, '');
  let matches = workingPara.match(/(genesis|exodus|leviticus|numbers|deuteronomy|joshua|judges|ruth|samuel|kings|chronicles|ezra|nehemiah|esther|job|psalms|proverbs|ecclesiastes|songofsolomon|isaiah|jeremiah|lamentations|ezekiel|daniel|hosea|joel|amos|obadiah|jonah|micah|nahum|habakkuk|zephaniah|haggai|zechariah|malachi|matthew|mark|luke|john|acts|romans|corinthians|galatians|ephesians|philippians|colossians|thessalonians|timothy|titus|philemon|hebrews|james|peter|john|jude|revelation)/g).join(', ');

  return matches;

  }

  findBooks();

我本来打算将圣经的书按数组列出,然后将它们与消息匹配,但发现我一次只能写一本书,所以我不想手动地逐一检查。我想遍历整个数组,但是没有用。我知道最终的答案是16,并想在答案的前面添加一个数字列表,但同样,它没有用。还希望将书的第一个字母大写,但要意识到整个答案是一个完整的字符串,因此无法使用charAt [0] .toUpperCase()方法。

对我的代码生成的答案很满意,但是增强会更好。 (例如1个Mark,2个John等)

3 个答案:

答案 0 :(得分:0)

您可以改用正则表达式的word boundaries (\b)。您的代码将变为:

return text.match(new RegExp('\\b(?:genesis|exodus|...|revelation)\\b', 'gi'));

g标志检索字符串中的所有匹配项,而i标志使其不区分大小写。

我还建议将书籍列表存储在一个数组中,然后返回一个数组,这更加灵活。这也使您可以使用索引来确定每个单词出现的位置。

const paragraph = `I once made a remark about the hidden books of the Bible. A certain luke, kept people looking so hard for facts, and for others, it was a revelation. Some were in a jam, especially since the names of the books were not capitalized. But the truth finally struck home to numbers of our readers. To others it was a job. We want it to be a most fascinating little moment for you. Yes, there will be some really easy ones to spot. Others may require judges to help find them. I will be quickly admit it usually takes a minister to find one of them, and there will be loud lamentations when it is found. A little lady says she brews a cup of tea so she can concentrate better. See how well compete. Relax now, for there really are sixteen books of the Bible in this paragraph.`;

function findBooks(text) {
  const books = ['genesis', 'exodus', 'leviticus', 'numbers', 'deuteronomy', 'joshua', 'judges', 'ruth', 'samuel', 'kings', 'chronicles', 'ezra', 'nehemiah', 'esther', 'job', 'psalms', 'proverbs', 'ecclesiastes', 'songofsolomon', 'isaiah', 'jeremiah', 'lamentations', 'ezekiel', 'daniel', 'hosea', 'joel', 'amos', 'obadiah', 'jonah', 'micah', 'nahum', 'habakkuk', 'zephaniah', 'haggai', 'zechariah', 'malachi', 'matthew', 'mark', 'luke', 'john', 'acts', 'romans', 'corinthians', 'galatians', 'ephesians', 'philippians', 'colossians', 'thessalonians', 'timothy', 'titus', 'philemon', 'hebrews', 'james', 'peter', 'john', 'jude', 'revelation'];
  return text.match(new RegExp('\\b(?:' + books.join('|') + ')\\b', 'gi'));
}

const foundBooks = findBooks(paragraph);
console.log(foundBooks);

const foundBooksPositions = foundBooks
  .map((book, position) => `${position + 1}: ${book}`)
  .join('\n');
console.log(foundBooksPositions);

答案 1 :(得分:0)

如果您只需要一个升序的数字序列,则可以在每个书名之前(在数组中)添加一个数字,就可以在数组上循环并在前面添加一个增量计数器,如下所示:

var matches = findBooks();
matches.forEach((book, index, arr) => {
    arr[index] = (index + 1) + " " + book;
});

答案 2 :(得分:0)

const paragraph = "I once made a remark about the hidden books of the Bible. A certain luke, kept people looking so hard for facts, and for others, it was a revelation. Some were in a jam, especially since the names of the books were not capitalized. But the truth finally struck home to numbers of our readers. To others it was a job. We want it to be a most fascinating little moment for you. Yes, there will be some really easy ones to spot. Others may require judges to help find them. I will be quickly admit it usually takes a minister to find one of them, and there will be loud lamentations when it is found. A little lady says she brews a cup of tea so she can concentrate better. See how well compete. Relax now, for there really are sixteen books of the Bible in this paragraph.";

function findBooks() {
    // regexp can be told to match case-insensitively by using the i flag
    const workingPara = paragraph.replace(/[., ]/ig, '');
    const matches = workingPara.match(/(genesis|exodus|leviticus|numbers|deuteronomy|joshua|judges|ruth|samuel|kings|chronicles|ezra|nehemiah|esther|job|psalms|proverbs|ecclesiastes|songofsolomon|isaiah|jeremiah|lamentations|ezekiel|daniel|hosea|joel|amos|obadiah|jonah|micah|nahum|habakkuk|zephaniah|haggai|zechariah|malachi|matthew|mark|luke|john|acts|romans|corinthians|galatians|ephesians|philippians|colossians|thessalonians|timothy|titus|philemon|hebrews|james|peter|john|jude|revelation)/ig);

    return matches.reduce((acc, book, idx) => {
        const numbering = idx + 1;

        // perform the toLowerCase method here instead of lowerCasing the whole paragraph
        // because the length of the paragraph may be very long, performing the method
        // here saves some computation time, although negligible in most cases
        const lowerCasedBook = book.toLowerCase();
        const capitalizedBook = lowerCasedBook[0].toUpperCase() + lowerCasedBook.slice(1);

        // yields books with numberings, e.g. 1 Mark
        if (idx !== matches.length - 1) {
            return acc += `${numbering} ${capitalizedBook}, `;
        } else {
            return acc += `${numbering} ${capitalizedBook}`;
        }
    }, '');
}

console.log(findBooks());
相关问题