Created
March 19, 2018 19:44
-
-
Save sdwvit/9bf7d5a0ab5b11f6f01cec3ba216b2e1 to your computer and use it in GitHub Desktop.
Convert text to sentence case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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