1

I wish to capitalize a first character of word that appears after a special character from specified set (array) is found in the string. Also I want ignore any number of spaces that it might have after the character from the array is found in the string but still capitalize the character after the special characters from the set.

Here is my code that I tried but not working:

function capitalizeChar() {
  var wordAfter = ["-", ":", "—", ".", "?", "!"];
  var c = false;
  var words = "welcome:  to the universe.";
  var charactercheck = words.split("");
  for (var i = 0; i < charactercheck.length; i++){
  if (c == true ) {
    if (charactercheck[i]!= ""){
      charactercheck[i] = charactercheck[i].toUpperCase(); 
      c=false;  }
    }
    for (var j = 0; j < wordAfter.length; j++) {
        if (charactercheck[i] == wordAfter[j]) {
        c = true; break;
        }
    }
  } 
  console.log(charactercheck.join(""));
  }
capitalizeChar();

But for some reason this code does not seem to be capitalize the 't' of 'To' which appears after the colon ':'. Can someone help, please!

MITH_N
  • 33
  • 5
  • Does this answer your question? [How can I capitalize the first letter of each word in a string using JavaScript?](https://stackoverflow.com/questions/32589197/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript) – Justinas Jun 19 '23 at 10:30
  • I tried that code but that is dependent on the spaces in a string. In my code above, I want to capitalize the first character of any single word that appears after the special characters from the list irrespective of whether there are spaces or not in between them. – MITH_N Jun 19 '23 at 10:41
  • Please scroll down. There is examples with regex - that is what you are looking for – Justinas Jun 19 '23 at 10:44
  • `s.replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) => a + b.toUpperCase())` – Dimava Jun 19 '23 at 10:50
  • The regex ones also consider space as a delimiter and most of them are very suitable for that scenario where there are spaces while some other examples in there capitalize every word split by spaces. In my example, I wish to completely ignore spaces. – MITH_N Jun 19 '23 at 10:52
  • Hi, @Dimava thanks for the code but that did not work. Also, how do I apply it to all the special characters from the array? charactercheck[i] = charactercheck[i].replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) => a + b.toUpperCase()); – MITH_N Jun 19 '23 at 10:55
  • Hey @Dimava, Sorry I misunderstood your regex solution. Below I applied it correctly. And it worked thanks a lot. console.log (words.replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) => a + b.toUpperCase())); – MITH_N Jun 19 '23 at 11:04
  • @MITH_N feel free to explain how that works in an answer (im lazy) – Dimava Jun 19 '23 at 11:24

2 Answers2

2

Here is how I solved it by using Dimava 's regex. But Alexander's solution was spot on.

function capitalizeChar() {
  var words = "welcome:  to the universe.";
  console.log(words.replace(/([-:—.?!]\s*)(\w)/g, (s, a, b, c) => a + b.toUpperCase()));
}
capitalizeChar();
MITH_N
  • 33
  • 5
0

Use a regular expression, should be pretty fast:

function capitalizeChar(str, charAfter = ["-", ":", "—", ".", "?", "!"]) {
    const regex = new RegExp(`(?<=[${charAfter.join('')}]\\s*)[a-z]`, 'g');
    return str.replace(regex, s => s.toUpperCase());
}

console.log(capitalizeChar('welcome:  to the universe. and -enjoy your ! suffering.'));
Alexander Nenashev
  • 8,775
  • 2
  • 6
  • 17