给定字符串'aAabBbcCc'
输出 'AaaBbbCcc'
str.toUpperCase().replace(/([A-Z])(\1+)/g, (m, p1, p2) => `${p1}${p2.toLowerCase()}`)
测试:
你给的demo里,上面有4个a,在下面变成了3个,是写错了还是?
如果只是想把字符串排序成大写字母在前面的话:
const str = 'aAabBacCc';
let arr = str.split('');
arr.sort().sort(function (s1, s2) {
x1 = s1.toUpperCase();
x2 = s2.toUpperCase();
if (x1 < x2) {
return -1;
}
if (x1 > x2) {
return 1;
}
return 0;
});
const newStr = arr.join('')
const result = [...'aAabBacCc'].map((value, index) => {
if (index === 0) {
return value.toUpperCase();
}
return value;
}).join('');
console.log(result);
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
仅仅针对指定字符串