0

I am trying to uppercase the first letter of each word in a sentence.

In javascript, \b seems to be intended for that : it matches the beginning of a word.

This works with ascii characters :

static capitalize(str: string): string {
  return str.replace(new RegExp('\\b\\w', 'g'), (txt) => txt.toUpperCase());
}

This test passes :

expect(StringUtils.capitalize('jean-michel dupont')).toEqual('Jean-Michel Dupont');

Problem : when I use latin characters, the latin character and the next character are uppercased.

StringUtils.capitalize('Fais de ta vie un rêve, et d\'un rêve, une réalité.')

Fais De Ta Vie Un RÊVe, Et D'un RÊVe, Une RÉAlité. // Actual

Fais De Ta Vie Un Rêve, Et D'Un Rêve, Une Réalité. // Expected

Is there an option to handle that ?

Arnaud Denoyelle
  • 25,847
  • 10
  • 73
  • 128
  • 1
    See https://stackoverflow.com/questions/49793359/javascript-regex-to-get-first-character-of-each-word-in-a-sentence-persian-and/49793589#49793589 – Wiktor Stribiżew Oct 11 '19 at 15:28

2 Answers2

1

Are you able to use CSS instead of JavaScript? If so, have a look at the text-transform CSS attribute.

Example:

<div style="text-transform: capitalize">Fais de ta vie un rêve, et d'un rêve, une réalité.</div>
Soc
  • 5,238
  • 3
  • 10
  • 25
  • Same as above, you missed the `u` -> `U` in `d'un rêve` should become `D'Un Rêve`. – Toto Oct 12 '19 at 11:52
  • I don’t actually know the rules about capitalization after an apostrophe in latin. However, in English, I don’t expect the following to be capitalized after the apostrophe: `Don't`, `Friend's`. There may be a definitive set of rules that you can implement yourself as a regular expression, but you will need to weigh up the cost/effort to do this yourself. – Soc Oct 13 '19 at 08:03
  • I don't want to do it myself, that's not my problem but OP requierement – Toto Oct 13 '19 at 12:25
  • FYI that's not Latin but French. – Toto Oct 13 '19 at 12:26
  • Do you spell `O'Connor` or `O'connor`? – Toto Oct 13 '19 at 13:21
  • @Soc this use case is very interesting. In `Don't` and `Friend's`, it would not make sense to capitalize `s`. But In French, `Jean d'Ormesson` means `John from Ormesson` so it makes sense to capitalize the `O`. This means that I will not find a css rule that does exactly what I want because it is locale sensitive. – Arnaud Denoyelle Oct 14 '19 at 09:02
  • Thanks all for pointing out this out. In a perfect world, this and other libraries will be locale aware. I was hoping that this would be the case here. – Soc Oct 14 '19 at 20:32
0

Try to use (?:^|\\s) instead of \b (from https://stackoverflow.com/a/10590516/11541562)

function capitalize(str) {
  return str.replace(new RegExp("(?:^|\\s)\\w", "g"), txt => txt.toUpperCase());
}

console.log(capitalize("Fais de ta vie un rêve, et d'un rêve, une réalité."));
f278f1b2
  • 274
  • 1
  • 7