Replacing question mark with exclamation mark and vice versa

javascript arrays string character

936 观看


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 ={
    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)


140 作者的声誉

You need to replace the two characters at the same time.

Take a look HERE (the second example)

作者: Lorenzo Grossi 发布者: 27.12.2017 04:41


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));

作者: acdcjunior 发布者: 27.12.2017 04:44


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.

作者: Davide Cannizzo 发布者: 27.12.2017 04:47