0 votes
in Education by (1.7m points)
I am looking for a solution that capitalizes each word of the input string to cell seperated by blank space or dot (similar to proper function). I know it is broken but what I tried so far:

    /*Capitalize Firt Letter of Each Word of input String in Cell*/

  if(activeRow > 1 && activeCol == 3 && ss.getSheetName() == validSheet && activeCell.isBlank() == false)


    var inputVal = activeCell.getValue().toString();





Input for cell A2:

this tExt neEds to be fixed

Output (Desired Result) for cell A2:

This Text Needs To Be Fixed

Thanks in advance

Edit 1: I noticed that proper function won't work because it requires cell value in it.

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.7m points)
Here's a function that takes a string and capitalizes the first letter of each word:

function capitalizePhrase(phrase) {

  var reg = /\b(\w)/g;

  function replace(firstLetters) {

    return firstLetters.toUpperCase();


  capitalized = phrase.replace(reg, replace);

  return capitalized;


which you could then use like this:

var inputVal = activeCell.getValue().toString();

var outputVal = capitalizePhrase(inputVal);


Edit - if you also want to set other letters in the word to lower case, you can use this function instead:

function properCase(phrase) {

  var regFirstLetter = /\b(\w)/g;

  var regOtherLetters = /\B(\w)/g;

  function capitalize(firstLetters) {

    return firstLetters.toUpperCase();


  function lowercase(otherLetters) {

    return otherLetters.toLowerCase();


  var capitalized = phrase.replace(regFirstLetter, capitalize);

  var proper = capitalized.replace(regOtherLetters, lowercase);

  return proper;