我有驗證下面的場景檢測功能,但功能使用的地圖和過濾方法。所以its not working in IE8
。如何重寫這個功能,而無需使用地圖和過濾器。定製JavaScript驗證不使用地圖,過濾
var case1 = "stack(2),flow(2),over(4),temp(7)"; - true
var case2 = "stack(2),flow(3),over(4),temp(k)"; - false
var case3 = "stack(2),flow(2),over(4),temp(0"; - false
var case4 = "stack(2),flow(2),over(,temp)"; - false
var case5 = "stack(2),flow(2),over(4)temp(8)"; - false
var case6 = "stack(1),flow(7),over,temp"; - true
var case7 = "stack(1),flow(7),OVER,Temp"; - true
var case8 = "stack(1),flow(7),over_r,temp_t"; - true
JavaScript函數:
function validateChunk(s)
{
return !!s.match(/^[a-z]+(?:\(\d+\))?$/);
}
function filterValid(v)
{
return !v;
}
function testCases(str)
{
var chunks = str.split(",");
var validated = chunks.map(validateChunk);
return (0 === validated.filter(filterValid).length);
}
+1其實你的循環似乎比原來更好的解決方案。 – kapa
很好,它的作品很好。同樣感謝簡化了這個方法。 – user2848031