Replacing question mark with exclamation mark and vice versa
936 观看
3回复
552 作者的声誉
I need to write a function that receives a sentence string and a true/false 'shouting' argument. It should return the string, replacing any question marks with exclamation marks and vice versa. If 'shouting' is true, all letters should be in uppercase.
function changeIntonation(str, isShouting) {
let myString = '';
let regExcl = /\!/;
let regQmark = /\?/;
let qMarkStr = str.replace(regQmark, '!');
let finalStr = qMarkStr.replace(regExcl, '?');
let newArr = finalStr.split('');
if(isShouting === true) {
let upperCaseArr = newArr.map(function(char){
return char.toUpperCase();
})
myString = upperCaseArr.join('');
}
return myString;
}
My code works when I try to convert Hey! How are you? to HEY?HOW ARE YOU!, but when I test Hey? How are you! it just makes it upper case and the exclamation and question mark remain unchanged. Also !? to ?! fails. Any ideas? Thank you.
作者: squeekyDave 的来源 发布者: 2017 年 12 月 27 日回应 3
1像
140 作者的声誉
You need to replace the two characters at the same time.
Take a look HERE (the second example)
作者: Lorenzo Grossi 发布者: 2017 年 12 月 27 日2像
91809 作者的声誉
The problem is when you try to replace !
with ?
and then replace ?
with !
, you risk replacing, by mistake, original !
and ?
that existed.
To work around that, you have to replace !
and ?
in one command, like (using a function argument on String#replace()
):
let finalStr = str.replace(/[!?]/g, function(c) { return c === '?' ? '!' : '?'; });
Also you don't need to call .toUpperCase()
on each char, you can use it on the string:
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));
1像
1018 作者的声誉
Thanks to @acdcjunior, who answered just before me and I didn't notice he already answered! My code looks like his, but is a little bit more compact.
function changeIntonation(str, isShouting) {
str = str.replace(/\?|\!/g, function(match) {
return match == "?" ? "!" : "?";
});
return isShouting ? str.toUpperCase() : str;
}
First of all I noticed you're making single characters upper-case to then join them all in a whole string. Why? I just made the whole string directly upper-case.
Then I understood the problem is that when you replace question marks with exclamation marks both those before were question and exclamation marks now are only exclamation marks, so if you try replacing exclamation marks with question ones, you'll replace them all.
So I choosed to replace with a callback both question and exclamations marks and check in the callback if the occurrence is the one or the other to replace correctly.
来自类别的问题 :
- javascript 如何检测网页中使用了哪一个定义的字体?
- javascript JavaScript对象的长度
- javascript 从下拉框中获取文本
- javascript 带隐藏按钮的登录脚本
- javascript 如何使用Prototype自动调整textarea?
- arrays 如何从C#数组中删除重复项?
- arrays 如何在C中确定数组的大小?
- arrays 在Ruby中将数组转换为散列的最佳方法是什么?
- arrays Comparing two byte arrays in .NET
- arrays 可以在MATLAB中完成并行遍历,就像在Python中一样吗?
- string C#中字符串和字符串有什么区别?
- string 为什么Ruby没有真正的StringBuffer或StringIO?
- string 在C#中将字符串转换为枚举
- string 将List <Integer>转换为List <String>
- string 连接字符串最有效的方法?
- character 在Java中迭代字符串的字符最简单/最佳/最正确的方法是什么?
- character 如何在Java中串联字符?
- character 是否有一个颠倒的插入符号?
- character 如何在控制台应用程序(Linux)中擦除打印的字符?
- character 如何在LIKE子句中转义方括号?