0

In javascript, I need to capitalize each first letter of words in a string (proper name) but not when before an apostrophe such as this example :

from henri d'oriona --> Henry d'Oriona

All I can get is something like Henry D'oriona or best case Henry D'Oriona.

Thanks for your help

Jacques

  • 1
    And what did you try till now? – Mara Black Dec 10 '19 at 14:47
  • [This may help](https://stackoverflow.com/questions/41048336/auto-capitalize-only-the-first-letter-of-each-word-in-an-input-field/41048432#41048432), but it doesn't take apostrophes into account. Shouldn't be too hard to adapt. – Scott Marcus Dec 10 '19 at 15:01

3 Answers3

1

You could use the String.replace method with a regular expression to produce a one-liner solution.

Note that String.replace can accept a function for its second argument which can be used to loop through the matches and modify them as necessary.

const specialCapitalize = 
 str => str.replace(/(\w[^\'])+/gi, word => word[0].toUpperCase() + word.slice(1));

console.log(specialCapitalize("henri d'oriona some other words"))

The regular expression /(\w[^\'])+/ is made up of a word character which is not followed by an apostrophe.

Danield
  • 121,619
  • 37
  • 226
  • 255
0

First case but to great was: toTitleCase('henri d'oriona')

returns Henri D'orina function toTitleCase(str) {

    return str.replace(/\w\S*/g,
    function(txt) {
            return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    }
    );

}

and best case scenario returning "Henri D'Oriona"

    function toProperCase(s) {
    return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
            function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();}
    return $1.toUpperCase(); });

}

0

Create a function which will make the first letter upperCase called capitalize. After that, split the string by empty space and traverse the array obtained.

For each element from the resulted array apply capitalize :

let upperCase = capitalize(word);

Then join the values from array into one string.

const capitalize = (s) => {

  if (typeof s !== 'string') return ''
  let capitalWord = '';

  if (s.includes("d'")) {

    let nameAfterSpecialChar = s.split("d'")[1];
    let capitalName = nameAfterSpecialChar.charAt(0).toUpperCase() + nameAfterSpecialChar.slice(1)
    capitalWord = "d'" + capitalName

  } else {
    capitalWord = s.charAt(0).toUpperCase() + s.slice(1)
  }

  return capitalWord;
}

const makeNameWithCapital = (name) => { 
  var wordsList = name.split(" ");
  let arrOfNames = []

  for (let word of wordsList) {
    let upperCase = capitalize(word);
    arrOfNames.push(upperCase);
  }

  let nameWithUpper = arrOfNames.join(" ");
  console.log(nameWithUpper)
  
  return nameWithUpper;
}

const nameUnparsed= "henri d'oriona "
const nameUnparsed1 = "henri d'oriona d'example test"

makeNameWithCapital(nameUnparsed)
makeNameWithCapital(nameUnparsed1)

It's not the best solution from performance point of view, but you can use it as a start.

Mara Black
  • 1,666
  • 18
  • 23