4
关注
2016
浏览

用感叹号代替问号

查看全部 3 个回答

solo 未验证用户 用户来自于: 广东省
2020-09-16 18:47

问题是,当你尝试用?更换!然后取代?!,你可能会更换,错误,原来!?已存在。

要解决这一点,你必须更换!?在一个命令,像(using a function argument on String#replace()):

let finalStr = str.replace(/[!?]/g, function(c) { return c === '?' ? '!' : '?'; }); 

而且你不需要调用.toUpperCase()每个字符,你可以用它字符串:

function changeIntonation(str, isShouting) { 
 
    let finalStr = str.replace(/[!?]/g, function(c) { return c === '?' ? '!' : '?'; }); 
 

 
    if (isShouting === true) { 
 
    return finalStr.toUpperCase(); 
 
    } else { 
 
    return finalStr; 
 
    } 
 
} 
 
console.log(changeIntonation("Hey! How are you?", true)); 
 
console.log(changeIntonation("Hey! How are you?", false)); 
 
console.log(changeIntonation("Hey? How are you!", true)); 
 
console.log(changeIntonation("Hey? How are you!", false));

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 16:02
关注人数
4 人关注

相关问题

边缘计算会代替云计算吗?
问号“?”在JS变量中
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号