【发布时间】:2022-11-24 00:25:19
【问题描述】:
如何在给定字符串中将 i 大写为 I:
const string = 'i have this. if i am. you and i and it is i';
我写了这个,但我认为必须有一个更好、更清晰的解决方案:
const string = 'i have this. if i am. you and i and it is i';
const res = string.split(' ').map(w => {
if(w == 'i') return 'I';
return w;
}).join(' ');
console.log(res)
【问题讨论】:
-
const res = string.split(' ').map(c => c === 'i' ? 'I' : c).join(' ');或const res = string.replace(/(?<!\S)i(?!\S)/g, 'I');
标签: javascript regex