我试图编写一个基于以下约束来替换案例的js函数:
对于每个单词,字母对于奇数位置应为大写,对于偶数位置应为小写。 Word可以包含字母,数字,空格。
我在互联网上找到了一些帮助,并且能够编写以下功能,但它适用于某些输入,但不适用于下面提到的其他功能。
function CaPiTaLiZe(s) {
var chars = s.toLowerCase().split("");
for (var i = 0; i < chars.length; i += 2) {
chars[i] = chars[i].toUpperCase();
}
console.log(chars.join(""))
return chars.join("");
}
// Giving expected o/p for the following i/p
CaPiTaLiZe('Hello') // ==> 'HeLlo'
CaPiTaLiZe('hello world') // ==> 'HeLlO WoRlD'
//But it does not give expected output for the following i/p.
CaPiTaLiZe('hello world testing 123') // ==> 'HeLlO WoRlD TeStInG 123'
CaPiTaLiZe(' Hello World ') // ==> ' HeLlO WoRlD '
CaPiTaLiZe(' I will see you in 2wo years') // ==> ' I WiLl SeE YoU In 2wO YeArS'
&#13;
注意:对于每个i / p,上面的箭头右侧(==&gt;)都有预期的o / p。
答案 0 :(得分:2)
您可以创建一个正则表达式来捕获单词并使用您的函数处理单个单词。
出于演示目的,我使用了正则表达式:/[a-z0-9]/gi
(假设一个单词中只包含字母数字字符。如果还有其他字符,请更新正则表达式。 )
以下是一个示例:
function toCapitalize(s) {
var chars = s.toLowerCase().split("");
for (var i = 0; i < chars.length; i += 2) {
chars[i] = chars[i].toUpperCase();
}
return chars.join("");
}
function CaPiTaLiZe(s) {
var parsedString = s.replace(/[a-z0-9]+/gi, toCapitalize)
console.log(parsedString)
return parsedString
}
// Giving expected o/p for the following i/p
CaPiTaLiZe('Hello') // ==> 'HeLlo'
CaPiTaLiZe('hello world') // ==> 'HeLlO WoRlD'
//But it does not give expected output for the following i/p.
CaPiTaLiZe('hello world testing 123') // ==> 'HeLlO WoRlD TeStInG 123'
CaPiTaLiZe(' Hello World ') // ==> ' HeLlO WoRlD '
CaPiTaLiZe(' I will see you in 2wo years') // ==> ' I WiLl SeE YoU In 2wO YeArS'
答案 1 :(得分:0)
我认为这就是你想要的。这首先遍历每个单词,然后使用正则表达式来大写单词中的替换字符。
另外,您最初不需要转换为小写。这将由函数本身来处理。
function CaPiTaLiZe(s) {
var words = s.split(/\s+/);
for (var i = 0; i < words.length; i++) {
words[i] = replace(words[i]);
}
console.log(words.join(" "));
return words.join(" ");
}
function replace(str) {
let first = true;
return str.replace(/./g, c => (first = !first) ? c.toLowerCase() : c.toUpperCase());
}
// Giving expected o/p for the following i/p
CaPiTaLiZe('Hello') // ==> 'HeLlo'
CaPiTaLiZe('hello world') // ==> 'HeLlO WoRlD'
CaPiTaLiZe('hello world testing 123') // ==> 'HeLlO WoRlD TeStInG 123'
CaPiTaLiZe(' Hello World ') // ==> ' HeLlO WoRlD '
CaPiTaLiZe(' I will see you in 2wo years') // ==> ' I WiLl SeE YoU In 2wO YeArS'
答案 2 :(得分:-1)
您的代码只将每个其他字符转换为大写字母。此代码段还将其他字符转换为小写字母。
function CaPiTaLiZe(s) {
var chars = s.toLowerCase().split("");
for (var i = 0; i < chars.length; i++) {
if ( i % 2 == 0 ) {
chars[i] = chars[i].toUpperCase();
} else {
chars[i] = chars[i].toLowerCase();
}
}
console.log(chars.join(""))
return chars.join("");
}
// Giving expected o/p for the following i/p
CaPiTaLiZe('Hello') // ==> 'HeLlo'
CaPiTaLiZe('hello world') // ==> 'HeLlO WoRlD'
//But it does not give expected output for the following i/p.
CaPiTaLiZe('hello world testing 123') // ==> 'HeLlO WoRlD TeStInG 123'
CaPiTaLiZe(' Hello World ') // ==> ' HeLlO WoRlD '
CaPiTaLiZe(' I will see you in 2wo years') // ==> ' I WiLl SeE YoU In 2wO YeArS'