0

I'm trying to convert a list of sentences to Title Case. The problem is however that with this regex the character does not get converted if it starts with a symbol. How can I get it to work?

This is a quick example:

var ls = ["this is fine" "+this is not"]
var response = ls.map(s => s.toLowerCase().replace(/^(.)|[^\w-'](\w)/gi, match => match.toUpperCase()))

I would like response to be ["This Is Fine" "+This Is Not"] and not ["This Is Fine" "+this Is Not"] like it is now.

In other words the problem is that +this is not capitalized. Same goes if I add "[this is a string]" or any other symbol.

Any ideas?

Costantin
  • 2,486
  • 6
  • 31
  • 48
  • Possible duplicate of [Convert string to title case with JavaScript](https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – vrintle Dec 11 '18 at 17:19

4 Answers4

1

You can use the \b metacharacter, which matches at any word boundary.

Then the regular expression becomes:

\b\w

i.e. "match any word character that follows a word boundary."

See it in action:

var ls = ["this is fine", "+this is not"];
var result = ls.map(s => s.toLowerCase().replace(/\b\w/gi, match => match.toUpperCase()));
console.log(result);
vrintle
  • 5,501
  • 2
  • 16
  • 46
Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
  • Hi @aurélien-gasser I thought this worked at the beginning, but then I noticed that it doesn't with non-English characters. Look at this https://jsfiddle.net/oyj63xec/ Do you know how I could catch and capitalize also those? Thank you! – Costantin Dec 15 '18 at 18:35
0

You can try this

^\W?\w|\W\w

Explanation

  • ^ - Anchor to start of string.
  • \W?\w - Matches any non word character. (? makes it optional). followed by any word character.
  • | - Alternation works same as logical OR.
  • \W\w - Matches any non word character followed by a word character.

Demo

var ls = ["this is fine", "+this is not"]
var response = ls.map(s => 
   s.toLowerCase().replace(/^\W?\w|\W\w/g , 
   match => match.toUpperCase()))
console.log(response);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You could match either a word character at the start of the string, or not a word character and then a word character:

^\w|\W(\w)

var ls = ["this is fine", "+this is not"];
var response = ls.map(s => s.toLowerCase().replace(/^\w|\W(\w)/g, match => match.toUpperCase()))
console.log(response);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

I understand you want to match the letters at the start of a word that is not preceded with - or '.

Thus, you may use

var ls = ["this is fine","+this is not", "man-of-war here", "I'm not"]
var response = ls.map(s => s.toLowerCase().replace(/(^|[^-'])\b([a-z])/g, 
    (_, g1, g2) =>  `${g1}${g2.toUpperCase()}`))
console.log(response);

The pattern will match

  • (^|[^-']) - Group 1: start of string or any char other than - and '
  • \b - a word boundary
  • ([a-z]) - a single ASCII lowercase letter
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563