Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert text to sentence case
<html><body><input id="in"></input>
<br />
<button id="convert" onclick="convert()">Convert to sentence case</button>
<div id="out"></div>
<script>
function toSentenceCase(t) {
t = t.toLowerCase();
for (var e = !0, n = "", o = 0; o < t.length; o++) {
var r = t.charAt(o);
/\.|\!|\?|\n|\r/.test(r) ? e = !0 : "" != r.trim() && 1 == e && (r = r.toUpperCase(), e = !1), n += r
}
return n = n.replace(/\bi\b/g, "I"), n = i(n)
}
function i(t) {
return t = t.replace(/\"([A-Za-z])/g, function(t) {
return t.toUpperCase()
})
}
function convert() {
var inp = document.getElementById("in").value;
document.getElementById("out").innerText = toSentenceCase(inp);
}
</script></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.