在 JavaScript 字符串数组中获取相邻字符的最佳方法是什么?

What's the best way to get adjacent characters in JavaScript array of strings?

本文关键字:字符 最佳 方法 是什么 字符串 JavaScript 数组 获取      更新时间:2023-09-26

假设我有这个:

var arr = [ "Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.", "The wizard quickly jinxed the gnomes before they vaporized.", "The quick brown fox jumps over the lazy dog."];

我试图找出一种方法,当我输入"jinxed"时,我也得到了"快速向导"和"之前侏儒"。不一定是整个单词,我只想在输入的旁边和之前获取几个字符。我一直在围绕这个碰壁,所以我感谢任何建议和提示。

谢谢!

function scan(arr, w) {
    // This function is a first attempt to solve your specific problem.
    // It just looks for the first occurence of w, the word you are looking for
    // It does not cover edge or specific cases, neither is optimized.
    // With more specifications, it can be generalized.
    // Hope it can help.
    var 
        // utils
        regWord = /'w+/g,
        wordsBefore, wordsAfter,
        matchWord,
        found = false;
    // loop through your array
    arr.forEach(function(item) {
    if (found) {
      // if we have already found the word, skip the iteration
      return false;
    }
    // clear the arrays of words before and after
    wordsBefore = []; 
    wordsAfter = [];
    // strip the words from the array item
    // every match is an iterable object containing the word, its index, and the item string
    while (match = regWord.exec(item)) {
      // get the matched word 
      matchWord = match[0].toLowerCase();
      // if we have found the word we are looking for
      if (matchWord === w.toLowerCase()) {
        // flag it
        found = true;
      } else {
        // it is not a match, but a word that could be returned in the array before or after
        if (!found) {
          // if we have not found the word yet
          // populate the array of words before
          wordsBefore.push(matchWord);
        } else {
          // otherwise, if we are here, it means that we are in a sentence containing
          // the word we are looking for, and we have already found it
          // populate the array of words after
          wordsAfter.push(matchWord);
        }
      }
    }
  });

  // return an object containing
  return {
    // the words before
    wordsBefore: wordsBefore,
    // the words after
    wordsAfter: wordsAfter 
  } 
}
使用

split 获取 jinxed 之前和之后的内容,然后使用 substr 获取每个部分中的最后一个字符或第一个字符:

var a = "The wizard quickly jinxed the gnomes before they vaporized";
var s = a.split('jinxed');
s == [ 'The wizard quickly ',' the gnomes before they vaporized' ]
s[0].substr(-10,10) == 'd quickly '
s[1].substr(0,10) == ' the gnome'

您可以使用正则表达式匹配:

function search(s,q, msg) {
  // match the characters before and after
  var m = s.match(new RegExp("(.|^)" + q + "(.|$)"));
console.log(m);
  // if there was a match at all, then drop the 1st (which is the whole match: " jinxed ")
  if (m) {
    if(m.length>1) {m = m.splice(1)}
    // m[0] in the character before, m[1] is the one after
    alert(msg + " '" + m[0] + "', '" + m[1] + "'");
  } else {
    alert(msg + " not found");
  }
}
search("The wizard quickly jinxed the gnomes before they vaporized.","jinxed", "in the middle");
search("jinxed the gnomes before they vaporized.","jinxed", "in the beginning");
search("The wizard quickly jinxed","jinxed","in the end");
search("jinxed","jinxed","full");
search("The wizard quickly","jinxed","not in string");

请注意,当您正在搜索的字符串位于行的开头或结尾时,这也有效。