跳过字符串中的空格
Skip whitespacing from string
如何在我的小脚本中跳过空白计数和着色?它在我的剧本中被计算在内,并被着色,我没有线索如何跳过它,而不是影响角色。所以重点是每两个字母都涂上颜色。有什么建议吗?
我知道如何使用jQuery,但我正在练习JavaScript。
JS Fiddle演示
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;
我建议您使用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
试试这个:
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];
}
}
我添加了一个偏移量变量,它将保持忽略空格的其他字母都应该着色的想法。
您可以将不想计数的东西列为白名单,比如空格,并使用单独的变量,而不是依赖于字符索引。
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];
}
}
这是小提琴。
似乎是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);
这是另一个选项:
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);
相关文章:
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 如何将字符串拆分为字符,但在javascript中保留空格
- 如何从字符串中删除除字母、数字、空格、感叹号和问号之外的所有内容
- 如何替换JavaScript字符串中除字母和数字之外的所有内容(空格/符号)
- JavaScript从字符串中删除尾随空格和句点
- 连接字符串时如何留出空格
- jQuery字符串使用split()方法在空格后拆分字符串
- JavaScript Regex,用于由特定字符串包围的数字和空格
- 为什么Javascript中的整数加空格加字符串会导致数字和字符串的总和?(2++'22'=24)
- Javascript在第一个空格上拆分字符串
- 截断带有剩余空格的字符串
- 字符串替换正则表达式,开头为空格
- JavaScript 从字符串中删除句号、逗号和空格
- 将字符串从第二个空格切成特殊字符
- 跳过字符串中的空格
- 如何在javascript中无空格地追加两个字符串
- 我的JS在将ascii转换为字符串时添加了一个空格
- 使用简单的角度滤波器来替换输入字符串中某些字符串的所有出现,而不考虑大小写和空格
- JS如何检查空字符串和空格
- 从数组中删除空字符串或空格字符串 - Javascript