搜索字符串并返回找到的单词

时间:2018-10-03 19:49:07

标签: javascript search

我想在字符串中搜索在数组书中找到的单词,然后返回找到的单词。我删除了特殊字符。但是我不知道如何搜索字符串。搜索返回-1

       <script>
var books = ['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy', 'Joshua', 'Judges'
, 'Ruth', 'Samuel', 'Samuel', 'Kings', 'Kings', 'Chronicles', 'Chronicles', 'Ezra', 'Nehemiah',
'Esther', 'Job', 'Psalms', 'Proverbs', 'Ecclesiastes', 'Song of Solomon', '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', 'Timothy', 'Titus', 'Philemon', 'Hebrews', 'James',
'Peter', 'Peter', 'John', 'Jude', 'Revelation'];



var puzzle = 'Can you find the names of 25 books of the Bible in this paragraph? This is a most remarkable puzzle.\
 Someone found it in the seat pocket on a flight from Los Angeles to Honolulu, keeping himself occupied for hours.\
  One man from Illinois worked on this while fishing from his john boat. Roy Clark studied it while playing his banjo. \
  Elaine Victs mentioned it in her column once. One woman judges the job to be so involving, she brews a cup of tea to \
  help calm her nerves. There will be some names that are really easy to spot that’s a fact. Some people will soon find \
  themselves in a jam, especially since the book names are not necessarily capitalized. The truth is, from answers\
  we get, we are forced to admit it usually takes a minister or scholar to see some of them at the worst. \
  Something in our genes is responsible for the difficulty we have. Those able to find all of them will hear \
  great lamentations from those who have to be shown. One revelation may help, books like! Timothy and Samuel \
  may occur without their numbers. And punctuation or spaces in the middle are normal. \
  A chipper attitude will help you compete. Remember, there are 25 books of the Bible lurking \
 somewhere in this paragraph. Greater love hath no man than this, that a man lay down his life for his friends. John 15:13.';
// Remove punctuation and spaces and set to lowercase

var matcher = /[a-z]+/gi;
var matches = puzzle.match(matcher);
var result = matches.join('');
var results = result.toLowerCase();
books = books.map(function (e) {
    return e.toLowerCase();
  });

//Search results for books and return those found
var i;
for (i = 0; i < books.length; i++) {
  var found =  puzzle.search(books)
  console.log(found);
}
      </script>

3 个答案:

答案 0 :(得分:0)

let reg = new RegExp('(' + books.join('|') + ')', 'gi');
let answer = puzzle.replace(/[^\w]/g,'').match(reg);
console.log(answer);

如果您在声明puzzle之后立即使用此代码,则它应该可以工作。

您可以执行循环和映射,也可以使用正则表达式来获取答案。

我只在Chrome开发者工具窗口中对此进行了测试,但是无论它在哪里运行,它都可以正常工作。

第一行将建立一个不区分大小写的正则表达式对象,并将搜索使用它的整个字符串。如果您做了console.log(reg),它看起来会像这样/(book1|book2|book3|...)/gi

第二行使用puzzle,删除所有非单词字符(puzzle.replace(/[^\w]/g,'')),然后与在第1行(.match(reg))上构建的正则表达式进行匹配。

答案 1 :(得分:-1)

尝试

this.state.date

答案 2 :(得分:-1)

由于有点困惑,这是解决问题的算法:

  1. puzzle字符串中,您应该删除除字母之外的所有字符。
  2. puzzle字符串中,将所有字母都小写。
  3. 也将books数组中的所有图书也都转换为小写。
  4. 对于books数组中的每个元素,都应使用puzzle.search(book)。如果结果不是负数,则这本书为文本。
  5. 将所有非负结果的图书收集到新数组中。

可以使用for循环或对reduce数组使用books方法来完成最后两个步骤。下面是示例,该如何完成:

puzzle = puzzle.replace(/\W/g,'').toLowerCase();

let result = [];

for(let i = 0; i < books.length; i++) {
  const index = puzzle.search(books[i].toLowerCase());
  if(index >= 0 && result.indexOf(books[i]) == -1) {
    result.push(books[i]);
  }
}

console.log(result);
console.log(result.length);