在javascript中搜索字符串中缺少字母表的字母

J. *_*son 9 javascript

我正在研究一些代码,这些代码将搜索字符串并返回缺少的字母表中的任何字母.这就是我所拥有的:

function findWhatsMissing(s){
    var a = "abcdefghijklmnopqrstuvwxyz";
    //remove special characters
    s.replace(/[^a-zA-Z]/g, "");
    s = s.toLowerCase();
    //array to hold search results
    var hits = [];

    //loop through each letter in string
    for (var i = 0; i < a.length; i++) {
        var j = 0;
        //if no matches are found, push to array
        if (a[i] !== s[j]) {
                hits.push(a[i]);
        }
        else {
            j++;
        }
    }
    //log array to console
    console.log(hits);
}
Run Code Online (Sandbox Code Playgroud)

但是使用测试用例:findWhatsMissing("dab c");

在将d添加到缺失数组之前的所有字母中的结果.

任何帮助将不胜感激.

Ada*_*ska 7

在循环中,您可以使用indexOf()以查看输入中是否存在该字母.像这样的东西会起作用:

for (var i = 0; i < a.length; i++) {
    if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!你可以看到它在这个JS小提琴中工作:https: //jsfiddle.net/573jatx1/1/