2013-12-09 46 views
0

如何跳過空格在我的小腳本中被計數和着色? 它被計算在我的腳本和彩色,我沒有線索如何跳過它,而不是影響字符。所以重點是每個第二個字母。有小費嗎 ?從字符串跳過空格

我知道我怎麼用jQuery來做,但我正在練習JavaScript。

JS Fiddle DEMO

var div = document.createElement('div'), 
    first = 'First JavaScript string.', 
    second = 'This is second text.', 
    combine = first + ' ' + second, 
    colored = ''; 

div.id = 'sort-text'; 
document.getElementsByTagName('body')[0].appendChild(div); 

for (i = 0; i < combine.length; i++) { 
    if (i % 2 == 1) { 
     colored += '<span style="color: #d10;">' + combine[i] + '</span>'; 
    } else { 
     colored += combine[i]; 
    } 
} 

div.innerHTML = colored; 
+0

以及你能只需將「for」循環的主體包裝在「if(combine [i]!==」「)'中,對吧?或者我錯過了一些東西......那當然不會抓住換行符,但是你也可以檢查'/ n'(我認爲可能有一些奇怪的操作系統特定的換行符) –

回答

2

我建議你使用map。這個問題比for循環更清晰。

例如:

var curr = -1; 
colored = combine.split("").map(function(x) { 
    if (x === " ") return x; // Ignore space 
    curr++; // Otherwise, increment 
    if (curr % 2 === 1) 
     return '<span style="color: #d10;">' + x + '</span>'; 
    return x; 
}).join(""); 

您可以輕鬆地擴展,以包括其他標點符號以及圖案。

例如,如果我們想忽略這兩個時期和空間,我們可以這樣做:

var curr = -1; 
var ignore = ". "; // Ignore space and period 
colored = combine.split("").map(function(x) { 
    if (ignore.indexOf(x) >= 0) return x; 
    curr++; 
    if (curr % 2 === 1) 
     return '<span style="color: #d10;">' + x + '</span>'; 
    return x; 
}).join(""); 

FIDDLE

+0

非常好的方法...我沒有'聽說.map()壽,所以我需要先閱讀它。感謝您的回答! – dvLden

+0

當然沒有問題:) – linstantnoodles

2

試試這個:

var space_offset = 0; 
for (i = 0; i < combine.length; i++) { 
    if (combine[i] == ' ') { 
     space_offset++; 
     colored += combine[i]; 
    } else if ((i + space_offset) % 2 == 1) { 
     colored += '<span style="color: #d10;">' + combine[i] + '</span>'; 
    } else { 
     colored += combine[i]; 
    } 
} 

我增加了一個偏移變量,將維持想法,其他字母忽略空格應該是彩色的。

2

你可以列出你不想計算的東西,比如空格,並且使用一個單獨的變量而不是依賴字符索引。

var useRed = false; 

for (i = 0; i < combine.length; i++) { 
    if (combine[i] != ' ') { 
     if (useRed) { 
      colored += '<span style="color: #d10;">' + combine[i] + '</span>'; 
      useRed = false; 
     } else { 
      colored += combine[i]; 
      useRed = true 
     } 
    } 
    else { 
     colored += combine[i]; 
    } 
} 

這裏是fiddle

1

似乎是continue任務:

var i, len, str, bol, curr_char, new_str; 

str = "hello wolrd this is a test."; 
new_str = ""; 
bol = false; 

for(i=0, len=str.length; i<len; i++){ 
    curr_char = str.charAt(i); 

    if(curr_char===" "){ 
     new_str += curr_char; 
     continue; 
    } 

    if(bol){ 
     new_str += "<span style='color: #d10;'>" + curr_char + "</span>"; 
    }else{ 
     new_str += curr_char; 
    } 

    bol = !bol; 
} 

console.log(new_str); 
1

這是另一種選擇:

var i, len, old_str, new_str, color_toggler, current_char, is_space; 

old_str = "hello world this is a test."; 
new_str = ""; 
color_toggler = true; 

for(i=0, len=old_str.length; i<len; i++){ 
    current_char = old_str.charAt(i); 
    is_space = (current_char===" "); 

    if(!is_space){ 
     color_toggler = !color_toggler; 
    } 

    if(color_toggler || is_space){ 
     new_str += current_char; 
    }else{ 
     new_str += "<span style='color: #d10;'>" + current_char + "</span>"; 
    } 
} 

console.log(new_str); 
+0

不錯,我喜歡這個......沒有特別的理由,它只是很容易理解。感謝兩個答案阿賈克斯! – dvLden

+0

也許你想將color_toggler設置爲false ...謝謝:) – dvLden