Skip to content

Instantly share code, notes, and snippets.

@sdwvit
Created March 19, 2018 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdwvit/9bf7d5a0ab5b11f6f01cec3ba216b2e1 to your computer and use it in GitHub Desktop.
Save sdwvit/9bf7d5a0ab5b11f6f01cec3ba216b2e1 to your computer and use it in GitHub Desktop.
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