循环中的Javascript循环字符测试

循环中的Javascript循环字符测试,javascript,jquery,loops,Javascript,Jquery,Loops,我有一个循环,通过一个巨大的字符串。对照另一个字符串中的各个数字检查每个数字,并突出显示匹配项 var decypher = "782137829431783498892347847823784728934782389"; var systemPass = "789544"; for (var x = 0; x < decypher.length; x++) { //loop through the array var switcher = 0; //not run this

我有一个循环,通过一个巨大的字符串。对照另一个字符串中的各个数字检查每个数字,并突出显示匹配项

var decypher = "782137829431783498892347847823784728934782389";

var systemPass = "789544";

for (var x = 0; x < decypher.length; x++) { //loop through the array
    var switcher = 0; //not run this row yet
    for (var p = 0; p < systemPass.length; p++) { //loop through each digit in the password
        if(eval(decypher[x]) === eval(systemPass[p])) { //if the password digit matches the array digit
            if (switcher === 0) { //not run yet...
                $('body').append("<p style='color: green; float: left;'>"+decypher[x]+"</p>");
                switcher = 1; //finished running
            }
        } else { //no match
            if (switcher === 0) { //not run yet...
                $('body').append("<p style='color: silver; float: left;'>"+decypher[x]+"</p>");
                switcher = 1; //finished running
            }
        } 
    }   
}
var decypher=“782137829431783498892347823784728934782389”;
var systemPass=“789544”;
对于(var x=0;x“+decypher[x]+”

”); 切换器=1;//已完成运行 } }否则{//不匹配 如果(切换器===0){//尚未运行。。。 $('body')。追加(“

”+decypher[x]+“

”); 切换器=1;//已完成运行 } } } }
JSFIDLE示例:

我的问题是,为什么它只突出了
7的
我已经为此挠头很久了

[编辑]
多亏了“@Yograj Gupta”-我已经删除了
切换器
变量,但现在我得到了每个字符的多个实例:

我在您的 我现在更新了代码,应该可以解决你的问题了

var decypher = "782137829431783498892347847823784728934782389";

    var pass = 789544;
    pass = "" + pass; var temp = '';
    for(var k =0; k < pass.length; k++){
      temp += pass[k] + '|';
    }
    temp = temp.substring(0, temp.length-1) ;

    console.log(new RegExp(temp,'g'));

    document.body.innerHTML =decypher.replace(new RegExp(temp,'g'), function(a){

        return '<span>'+a + '</span>';

    });

​
var decypher=“782137829431783498892347823784728934782389”;
var-pass=789544;
pass=”“+pass;var temp=“”;
对于(var k=0;k
它只显示7,因为在第一次迭代中,您将在内部循环中使switcher=1

因此,当它出现在7时,7就出现在systemPass变量的0索引上,所以在第一次迭代中,检查它是否存在,并将其显示为绿色。但所有它去为其他和显示在银色和切换器成为1有


所以,您应该对照indexOf函数检查您的值。

好吧,您肯定是在艰难地完成这项工作。使用
indexOf
(或者,正如约翰所指出的,
jQuery.inArray
):

var decypher=“782137829431783498892347823784728934782389”;
var systemPass=“789544”;
对于(变量x=0;x-1{//谢谢,Johan
if($.inArray(decypher[x],systemPass)>-1){
$('body')。追加(

“+decypher[x]+”

”); }否则{//不匹配 $('body')。追加(“

”+decypher[x]+“

”); } }
虽然这里还有很多其他的清理建议,但至少循环更容易

--斯科特检查这把小提琴:

注意:按照Scott Sauyet的建议,使用indexOf()更新了小提琴

代码:

for(var x=0;x“+decypher[x]+”

”; }
像这样?还是我错过了什么

var decypher = "782137829431783498892347847823784728934782389".split('');

var systemPass = "789544".split('');

$.each(decypher, function(i, v){

    if(systemPass.indexOf(v) !== -1)
        $('body').append("<p style='color: green; float: left;'>"+ v +"</p>");
    else
        $('body').append("<p style='color: silver; float: left;'>"+ v +"</p>");

});
var decypher=“782137829431783498892347823784728934782389”。拆分(“”);
var systemPass=“789544”。拆分(“”);
$。每个(decypher,函数(i,v){
if(systemPass.indexOf(v)!=-1)
$('body')。追加(

“+v+”

”; 其他的 $('body')。追加(

“+v+”

”; });

看看这个

我想这会有帮助的

说明: 在内部循环中分配切换器=1。因此,在第一次匹配之后,切换器为1,并且始终执行else部分。
你得到了多个字母,因为你将每个字母都附加到内部循环中的decypher,它被添加到systemPass.length times中。

我认为他实际上不想替换7。:)啊,我明白了!但是现在我得到了(每个字符的多个实例…)呸,比我快1分钟-你可以用
for(decypher中的var x)
替换for条件,用
if(~systemPass.indexOf(decypher[x]))替换if语句来进一步减少代码:)即使我们在回答中都使用了
indexOf
,也许最好使用
$.inArray()
让它在ie7中工作?@Johan-是的,可能是这样。我会编辑以合并。
for (var x = 0; x < decypher.length; x++) { //loop through the array
    var found = systemPass.indexOf(decypher[x]);
    $('body').append("<p style='color: "+(found !== -1?'green':'silver')+"; float: left;'>"+decypher[x]+"</p>");
}
var decypher = "782137829431783498892347847823784728934782389".split('');

var systemPass = "789544".split('');

$.each(decypher, function(i, v){

    if(systemPass.indexOf(v) !== -1)
        $('body').append("<p style='color: green; float: left;'>"+ v +"</p>");
    else
        $('body').append("<p style='color: silver; float: left;'>"+ v +"</p>");

});