Sentence case is a mixed case style that uses both uppercase and lowercase letters in sentences, headlines, and titles. For example:

This is a headline. New sentences start with a capital letter.

Allowing users to convert selected text to this style improves your application's user experience. The following sample uses regular expressions to find the indexes of a sentence pattern. Typically, a new sentence begins after a period followed by one or two spaces. While some users believe either way is correct, Microsoft formally settled this debate in 2020, siding with those who believe one space is correct, not two. Microsoft Word considers two spaces between sentences to be an error.

Either way, we use the following regular expression to match both situations:

/\.\ +/g

Sentence case

The following code uses the Selection TX Text Control .NET Server for ASP.NET
JavaScript API
Selection Object
Describes and handles the attributes of a text selection.
object to get the start offset index and the text of the current selection. The regular expression returns the matches that mark the end and the beginning of a sentence. For each match, the character at the following location index is replaced with its uppercase representation using the setText TX Text Control .NET Server for ASP.NET
JavaScript API
Selection Object
setText Method
Sets the currently selected text.
method.

function setSentenceCase() {
// get the start offset and the selected text
TXTextControl.selection.getStart(function(start) {
TXTextControl.selection.getText(function(curText) {
// in Text Control, CRLF is one character
text = curText.split("\r\n").join("\n");
// get all matches including indexes
var matchesAll = [...text.matchAll(/\.\ +/g)];
// to get the actual length, another match is required in JS
var matches = [...text.match(/\.\ +/g)];
const indexes = matchesAll.map(match => match.index);
// loop through array of indexes
for (let index = 0; index < indexes.length; ++index) {
const element = indexes[index];
var sel = TXTextControl.selection;
var bounds = { "start": matches[index].length + start + element, "length": 1 };
sel.setBounds(bounds);
sel.setText(text.charAt(matches[index].length + element).toUpperCase());
}
})
})
}
view raw test.js hosted with ❤ by GitHub

Happy coding!