Funtionality:
我有一个字符串A,4.0,00:04
。拆分字符串的主要目的是将00:04
的值附加到" min"的下一个div中。和" sec"因此,我需要进一步分割00:04
的值,这样当我附加值时,它将显示为" 00 min" " 04秒"。
问题:
此时我无法进一步分裂","当我在Split字符串上执行我的console.log时,它将返回A |的单个String元素4.0 | 00:04。
然而,在这一点上,我不知道如何进一步取出" 00"和" 04"附加到" min" &安培; "秒" div标签。
请帮忙。
var data = 'A,4.0,00:04';
console.log("BestTime: " + data);
//When I consolelog BESTTIME, it will return the element of (A, 4.0, 00:04)
var TimerList = data.split(",");
console.log("TimerList: " + TimerList[0] + "|" + TimerList[1] + "|" + TimerList[2]);
//TimerList[0] => A //Not What I want to append to div tag
//TimerList[1] => 4.0 // Not What I want to append to div tag
//TimerList[2] => 00:04 //Need to futher split this so that I can append 00 to Game_BestTimer_Minute & 04 to Game_BestTimer_Second
//append BEST GAME TIME to SCOREBOARD
$("#Game_BestTimer_Minute").html(TimerList[2]);
$("#Game_BestTimer_Second").html(TimerList[2]);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Game_BestTimer_Minute" style="font-family:'Avenir';"></div>
<div id="Game_BestTimer_Second" style="font-family:'Avenir';"></div>
&#13;
答案 0 :(得分:2)
您可以将String.prototype.match()
与RegExp
/\d+(?=:)|\d+$/g
匹配,以匹配一个或多个数字字符,后跟":"
或字符串末尾的数字,以获取包含{{1}的数组}。
使用["00", "04"]
处的多个选择器拨打jQuery()
.html(function)
var data = 'A,4.0,00:04';
var TimerList = data.match(/\d+(?=:)|\d+$/g);
console.log(TimerList);
$("#Game_BestTimer_Minute, #Game_BestTimer_Second")
.html(function(index, _) {
return TimerList[index]
});
答案 1 :(得分:1)
var data1 = "A,4.0,00:04";
var data = data1.split(",")[2]
var min = data.split(":")[0]
var sec = data.split(":")[1]
console.log("BestTime: " + data );
//var TimerList = data.split(",");
console.log("TimerList: " + data + "|" + min + "|" + sec);
//TimerList[0] => A //Not What I want to append to div tag
//TimerList[1] => 4.0 // Not What I want to append to div tag
//TimerList[2] => 00:04 //Need to futher split this so that I can append 00 to Game_BestTimer_Minute & 04 to Game_BestTimer_Second
//append BEST GAME TIME to SCOREBOARD
$("#Game_BestTimer_Minute").html(min);
$("#Game_BestTimer_Second").html(sec);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Game_BestTimer_Minute" style="z-index:10; position:absolute; top:450px; left:870px; font-size:160px; font-family:'Avenir'; width:1080px; color:#fff;"></div>
<div id="Game_BestTimer_Second" style="z-index:10; position:absolute; top:450px; left:1215px; font-size:160px; font-family:'Avenir'; width:1080px; color:#fff;"></div>
使用":"
答案 2 :(得分:1)
你能尝试如下吗?
var value = TimerList[2].split(':');
$("#Game_BestTimer_Minute").html(value[0]);
$("#Game_BestTimer_Second").html(value[1]);
答案 3 :(得分:1)
很好,但我想知道你为什么不再分裂。记住调用修剪以移除空间。
var TimerList = data.split(",");
console.log("TimerList: " + TimerList[0] + "|" + TimerList[1] + "|" + TimerList[2]);
var timerList = TimerList[2].split(":");
//append BEST GAME TIME to SCOREBOARD
$(&#34;#Game_BestTimer_Minute&#34)。HTML(timerList [0] .trim()); $(&#34;#Game_BestTimer_Second&#34)。HTML(timerList [1] .trim());
答案 4 :(得分:1)
再次使用冒号而不是逗号作为分隔符再次调用.split()
。
另请注意,使用.text()
比.html()
将值插入DOM要快得多。
var data = "A,4.0,00:04"
console.log("BestTime: " + data);
var TimerList = data.split(",");
console.log("TimerList: " + TimerList[0] + "|" + TimerList[1] + "|" + TimerList[2]);
//TimerList[0] => A //Not What I want to append to div tag
//TimerList[1] => 4.0 // Not What I want to append to div tag
//TimerList[2] => 00:04 //Need to futher split this so that I can append 00 to Game_BestTimer_Minute & 04 to Game_BestTimer_Second
var MinSec = TimerList[2].split(":");
//append BEST GAME TIME to SCOREBOARD
$("#Game_BestTimer_Minute").text(MinSec[0]);
$("#Game_BestTimer_Second").text(MinSec[1]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Game_BestTimer_Minute"></div>
<div id="Game_BestTimer_Second"></div>
&#13;
答案 5 :(得分:1)
似乎更适合使用正则表达式在字符串末尾查找\d+:\d+
...
var data = 'A,4.0,00:04';
console.log("BestTime: " + data);
var [match, min, sec] = data.match(/(\d+):(\d+)$/)
$("#Game_BestTimer_Minute").html(min);
$("#Game_BestTimer_Second").html(sec);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Game_BestTimer_Minute" style="font-family:'Avenir';"></div>
<div id="Game_BestTimer_Second" style="font-family:'Avenir';"></div>