Js - 常规匹配数组和字符串

时间:2014-02-06 15:01:31

标签: javascript arrays string expression match

第一篇文章,请温柔,请; D

代码:

var matchString;
var searchString = '';
var lastKey;
function keyDown(e) {
  if (!e)
    e = window.e;
    for (var i=0; i<asciiNum.length;i++){
        if (e.keyCode == asciiNum[i]){
            console.log('asciiLet: '+asciiLet[i]);
            lastKey = asciiLet[i];
            searchString +=lastKey;
        }else if(e.keyCode == "27"){
            searchString="";
            console.log(searchsrtring)
        }
    }
}

function keyUp(e) {
    console.log('left: '+searchString)
    for (var i=0; i<champArray.length; i++){
    }

}

数组:

var nameArray = ["aatrox","ahri","akali","alistar","amumu","anivia","annie","ashe", "blitzcrank",   "brand",    "caitlyn",  "cassiopeia",   "cho'gath", "corki",    "darius",   "diana",    "draven",   "dr. mundo",    "elise",    "evelynn",  "ezreal",   "fiddlesticks", "fiora",    "fizz", "galio",    "gangplank",    "garen",    "gragas",   "graves",   "hecarim",  "heimerdinger", "irelia",   "janna",    "jarvan iv",    "jax",  "jayce",    "karma",    "karthus",  "kassadin", "katarina", "kayle",    "kennen",   "kha'zix",  "kog'maw",  "leblanc",  "lee sin",  "leona",    "lissandra",    "lucian",   "lulu", "lux",  "malphite", "malzahar", "maokai",   "master yi",    "m.fortune",    "mordekaiser",  "morgana",  "nami", "nasus",    "nautilus", "nidalee",  "nocturne", "nunu", "olaf", "orianna",  "pantheon", "poppy",    "quinn",    "rammus",   "renekton", "rengar",   "riven",    "rumble",   "ryze", "sejuani",  "shaco",    "shen", "shyvana",  "singed",   "sion", "sivir",    "skarner",  "sona", "soraka",   "swain",    "syndra",   "talon",    "taric",    "teemo",    "thresh",   "tristana", "trundle",  "tryndamere",   "twitch",   "twisted fate", "udyr", "urgot",    "varus",    "vayne",    "veigar",   "vi",   "viktor",   "vladimir", "volibear", "warwick",  "wukong",   "xerath",   "xin zhao", "yorick",   "zac",  "zed",  "ziggs",    "zilean",   "zyra", "jinx"];
    var asciiNum = ['27','65','66','67','68','69','70','71','72','73','74','75','76','77','78','79','80','81','82','83','84','85','86','87','88','89','90']
    var asciiLet=  ['Esc','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

那么,我想做什么: 我想将 searchstring 的内容与 nameArray 匹配,实际上每个按下的键都匹配。 最后每个元素谁都没有以f.e开头。 “A”应该淡出。 希望你们都能理解我破碎的英语,并会对我表示同情(:

1 个答案:

答案 0 :(得分:0)

你可以这样做

  1. 将keyDown回调添加到输入(我假设它是HTMLInputElement

  2. 在keyDown回调延迟调用searchNameFromInput函数,以便在检查时输入的值已经改变。

  3. 通过创建一个正则表达式来检查nameArray中的元素,该正则表达式从头开始检查名称与输入值的匹配(例如:"an"将匹配"anivia"和{{ 1}})

  4. 代码如下所示

    "annie"