0

I'm just about pulling my hair here. I have a function that I made (found and modified) To make text title case. The reason I use this function Instead of just capitalizing the first letter of every word is because there are many words That should not be capitalized.

The code that I have here works. Now all I need is for it to capitalize the first character of every sentence. So after it finished is the function, create a new function that will capitalize the first character of the new string. I just can't figure out how to do this in his driving me crazy. Thank you so much for all your help guys I appreciate it.

var TITLECASE = function (str, glue) {
  glue = (glue) ? glue : ['a', 'and', 'as', 'at', 'but', 'by', 'for', 'if', 'in', 'is', 'it', 'my', 'nor', 'of', 'off', 'on', 'or', 'per', 'so', 'the', 'this', 'to', 'up', 'via', 'with', 'yet'];
  return str.replace(/(\w)(\w*)/g, function (_, i, r) {
    var j = i.toUpperCase() + (r != null ? r : "");
    return (glue.indexOf(j.toLowerCase()) < 0) ? j : j.toLowerCase();
  });
};
pilchard
  • 12,414
  • 5
  • 11
  • 23
Yerain
  • 1

1 Answers1

0

If you are looking to capitalize the first letter in any sentence you can try using charAt + toUppercase like so;

var input="hello this is a sentence";

var output=input.charAt(0).toUpperCase()+input.substr(1,test.length);
// Hello this is a sentence
Grant
  • 115
  • 1
  • 7
  • Hi and thank you so much for your comment. I'm trying what you've provided now. I think there's something wrong with my implementation. Also I noticed that you wrote "test" instead of "text" In one instance. This was a mistake correct? either way, here is the new code, still not working. :/. – Yerain Jan 21 '21 at 21:58
  • var TITLECASE = function(str, small){ small = (small) ? small : ['a','and','as','at','an','by','this','if','is']; return str.replace(/(\w)(\w*)/g, function(_, i, r){ var j = i.toUpperCase() + (r != null ? r : ""); var text = (small.indexOf(j.toLowerCase())<0)?j:j.toLowerCase(); return (text) }); line = text.charAt(0).toUpperCase()+text.substr(1,text.length); return (line); }; TITLECASE("this is an example ") – Yerain Jan 21 '21 at 21:59
  • I'm sorry if that wasn't the right place to put the code. I'm new to stack overflow. And I'm not sure where to paste the code that I have just added. – Yerain Jan 21 '21 at 22:00