-4

I've successfully followed the steps outlined at Convert string to Title Case with JavaScript however was wondering if there as a way for me to ensure certain words remain lowercase?

For example "THE university Of Maastricht" should become "The University of Maastricht" and "The queen Of The nile" should become "The Queen of the Nile".

This would mean excluding words which match "of" and "the" (in any capitalisation combination) except if they are the first word?

Paul
  • 19,704
  • 14
  • 78
  • 96
  • 1
    This is already explained in the question you linked: https://stackoverflow.com/a/46774740/1871033 or https://stackoverflow.com/a/64910248/1871033 – CherryDT Sep 21 '22 at 17:19
  • You wrote you want sentence case but your examples show title case so I assume you actually meant title case. Note that the grammar rules say that not only the first but also the _last_ word shouldn't be capitalized (which both linked answers handle correctly). – CherryDT Sep 21 '22 at 17:26
  • A nice read: https://www.tempertemper.net/blog/sentence-case-versus-title-case – Roko C. Buljan Sep 21 '22 at 17:52
  • 1
    @CherryDT Gah, I can't believe I missed that answer further down the page. It had been a very, very long day ... Apologies everyone but still props to Mina for taking the time to summarise and simplify. – Simon Logan Sep 22 '22 at 11:45
  • By the way, above of course I meant "should always be capitalized", not "shouldn't be capitalized" – CherryDT Sep 22 '22 at 11:49

1 Answers1

0

You can check if the indexOf the txt not 0 so it's not first word, and create a list of excluded words and also check if this list includes the txt, then return the txt as it is.

const lowerCaseList = ["of", "and"]

function toTitleCase(str) {
  return str.replace(
    /\p{L}+/gu,
    function(txt) {
      if (str.indexOf(txt) !== 0 && lowerCaseList.includes(txt.toLowerCase())) {
        return txt.toLowerCase();
      }
      return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    }
  );
}
<form>
  Input:
  <br /><textarea name="input" onchange="form.output.value=toTitleCase(this.value)" onkeyup="form.output.value=toTitleCase(this.value)"></textarea>
  <br />Output:
  <br /><textarea name="output" readonly onclick="select(this)"></textarea>
</form>

This answer extends from this answer.

Mina
  • 14,386
  • 3
  • 13
  • 26
  • 2
    This was already answered in the other question, no need to re-answer (or if you have another solution, it's probably better to answer over there so it won't get scattered). I'd close-vote but I accidentally retracted the vote so I can't. – CherryDT Sep 21 '22 at 17:23
  • Not that the question was long, but your output does not what has being asked for. – Roko C. Buljan Sep 21 '22 at 17:26
  • The above `/\w\S*/g` works only for languages strictly using word characters in the list of `\w` so a terribly short and exclusive range — even nonsensical of `[a-zA-Z0-9_]`. Quite a shame – Roko C. Buljan Sep 21 '22 at 17:32
  • @CherryDT, I thought making a simpler function and describing it carefully would be better as the answer you mentioned is correct but complex for a beginner. – Mina Sep 21 '22 at 17:33
  • @RokoC.Buljan, would you mind please describing why not related to the question?, maybe there is a misunderstanding. – Mina Sep 21 '22 at 17:34
  • @Mina read the question second paragraph carefully and test it in your demo. – Roko C. Buljan Sep 21 '22 at 17:35
  • 1
    @RokoC.Buljan, Thanks for notifying me about the issue, I fixed the bug about the excluded words, And I mentioned that my answer is extended from that answer. about the other issue, I think the OP target English only. – Mina Sep 21 '22 at 18:01
  • @Mina English also has words with accented characters (usually borrowed from French), such as `naïve` or `résumé`, and of course a sentence may contain foreign names such as `My vacation at Wörthersee was amazing`. – CherryDT Sep 22 '22 at 11:31
  • @CherryDT, thanks for mentioning that, but I was studying English for 16 years ( in school and college) and I never saw this case, maybe this exists in the vernacular, I'm not sure as I'm not a native English speaker, but anyway I think `\p{L}+` with `u` flag will do the trick. – Mina Sep 22 '22 at 12:21