Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to sentence case in javascript

I want a string entered should be converted to sentence case in whatever case it is.

Like

hi all, this is derp. thank you all to answer my query.

be converted to

Hi all, this is derp. Thank you all to answer my query.

like image 922
Mahendra Avatar asked Sep 30 '13 08:09

Mahendra


People also ask

How can I change to Title Case in JavaScript?

Method 1: Convert String to Title Case in JavaScript Using replace() Method. In JavaScript, the “replace()” method replaces the current string value with the updated one. This method can be used in combination with “toUpperCase()” for converting a string to the title case.

How do you write a sentence in JavaScript?

In JavaScript, there are three ways to write a string — they can be written inside single quotes ( ' ' ), double quotes ( " " ), or backticks ( ` ` ).


1 Answers

I came up with this kind of RegExp:

var rg = /(^\w{1}|\.\s*\w{1})/gi;
var myString = "hi all, this is derp. thank you all to answer my query.";
myString = myString.replace(rg, function(toReplace) {
    return toReplace.toUpperCase();
});
like image 198
Samuli Hakoniemi Avatar answered Sep 21 '22 07:09

Samuli Hakoniemi