How to transform the first letter of words into a string

Asked

Viewed 1,053 times

10

Having a given string, how to put in large letter (High Box) the first letter of each word?

For example:

original: 'a música é universal'
Caixa Alta: 'A Música É Universal'

but also names like "anna-karin" common good in the Nordic countries. In that case it should be "Anna-Karin".

There is a solution for all languages?

2 answers

9


The solution I found, which works for most language accents latinas and germanic, was with regex:

string.replace(/([^A-zÀ-ú]?)([A-zÀ-ú]+)/g, function(match, separator, word){
    return separator + word.charAt(0).toUpperCase() + word.slice(1);
});

In the regex I try to find one character not on this list [A-zÀ-ú] (optional, hence the ?) and a word of one or more letters with [A-zÀ-ú]+, both with capture group to work within the replace function.

Example:

String.prototype.capitalize = function(){
    return String(this).replace(/([^A-zÀ-ú]?)([A-zÀ-ú]+)/g, function(match, separator, word){
        return separator + word.charAt(0).toUpperCase() + word.slice(1);
    });
}

alert('a anna-karin gosta de música!'.capitalize());

2

Another possibility, created by the PHP.JS staff based on the function ucwords() of PHP:

function ucwords(str) {
  //  discuss at: http://phpjs.org/functions/ucwords/
  // original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // improved by: Waldo Malqui Silva
  // improved by: Robin
  // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // bugfixed by: Onno Marsman
  //    input by: James (http://www.james-bell.co.uk/)
  //   example 1: ucwords('kevin van  zonneveld');
  //   returns 1: 'Kevin Van  Zonneveld'
  //   example 2: ucwords('HELLO WORLD');
  //   returns 2: 'HELLO WORLD'

  return (str + '')
    .replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function($1) {
      return $1.toUpperCase();
    });
}

Example:

function ucwords(str) {
  //  discuss at: http://phpjs.org/functions/ucwords/
  // original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // improved by: Waldo Malqui Silva
  // improved by: Robin
  // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // bugfixed by: Onno Marsman
  //    input by: James (http://www.james-bell.co.uk/)
  //   example 1: ucwords('kevin van  zonneveld');
  //   returns 1: 'Kevin Van  Zonneveld'
  //   example 2: ucwords('HELLO WORLD');
  //   returns 2: 'HELLO WORLD'

  return (str + '')
    .replace(/^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g, function($1) {
      return $1.toUpperCase();
    });
}

alert( ucwords( 'num ninho de mafagafos tinha sete mafagafinhos' ) );

  • Good suggestion, but fails in names like "Anna-Karin" or "O'Leary". For example the phrase "a anna-karin e o o'leary estão contentes" gives "A Anna-karin E O O'leary Estão Contentes".

  • Because it is a JS version of a language function they had to respect the output format of the original function. And, perhaps because technically "Anna-Karin" and "O'Leary" are a single word, this is what ucwords() from PHP returns.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.