PDA

View Full Version : Sentence Caps Script


Philip M
04-15-2008, 09:55 AM
The following converts text to sentence caps (i.e. lower case except for first character of a sentence). Proper names such as A.B.Smith are correctly capitalised. Times such as 9.00p.m. correctly displayed.

<textarea name = "txt1" id = "txt1" rows = 8 cols = 60></textarea>

<script type = "text/javascript">

//Assumes that a sentence ends with . ! or ? followed by space(s). Proper names such as a.b.macdonald correcty capitalised. Times such as 9.00p.m. correctly displayed.

var txt = "lorem ipsum DOLOR sit amet. consectetur a.b.macdonald adipisicing elit! 9.00p.m. sed do EUISMOD tempor? incididunt ut labore et dolore magna aliqua. ut enim r.b.a.robinson ad minim! veniam, quis nostrud exercitation. ullamco laboris nisi? ut aliquip ex ea commodo consequat. semper et ubi in excreto sunt! c.brutus."

txt = txt.replace(/^\s+/,""); //strip leading spaces
txt = txt.replace(/\s+$/,""); // strip trailing spaces
txt = txt.replace(/\s{4}/g,"~~~ "); // preserve 4 8 12 etc. spaces
txt = txt.replace(/\s{3}/g,"~~ "); // preserve 3 6 7 9 11 spaces
txt = txt.replace(/\s{2}/g,"~ "); // preserve 2 5 10 spaces
// if ~ character appears in text, replace by @ or } or whatever.
txt = txt.toLowerCase(); // delete this line if existing upper case characters in the text are to be preserved
txt = txt.replace(/\b([a-z{1}]\.)([a-z{1}])/gi,function(w){return w.toUpperCase()}); // convert a.b.s to UC
var len = txt.length;

var newstr = new Array(len);
newstr = txt.split("");
newstr[0] = newstr[0].toUpperCase(); // 1st character

for (var i = 0; i <= len-2; i++) {
if ((newstr[i] == ".") || (newstr[i] == "!") || (newstr[i] == "?") || (newstr[i] == "~")) {
newstr[i+2] = newstr[i+2].toUpperCase();
}
}

newtxt = newstr.join("");
newtxt = newtxt.replace(/~/g," ");

newtxt = newtxt.replace(/(\.[A-Za-z][A-Z])/g,function(w){return w.toLowerCase()}); // eliminate two consecutive capital letters or capital after lower case following period
newtxt = newtxt.replace(/(\.[a-z])/g,function(w){return w.toUpperCase()}); // first letter after . back to capital
newtxt = newtxt.replace(/([a-z]\.[a-z]\.\s)/gi,function(w){return w.toLowerCase()}); // fix a.m., p.m., i.e., e.g. etc.

alert (newtxt);
document.getElementById("txt1").value = newtxt;
document.write("<br><br>" + newtxt); // note HTML suppresses multiple spaces
alert (txt.length + " " + newtxt.length); // strings same length so spaces were preserved!
</script>

shyam
04-20-2008, 07:11 PM
merge everything into a single regex?
txt.replace(/((\b|)([a-z]\.)([a-z])(\b|)|(^|[.?!]\s*)(\w))/gi,function(w){return w.toUpperCase();})

Philip M
05-01-2008, 05:50 PM
merge everything into a single regex?
txt.replace(/((\b|)([a-z]\.)([a-z])(\b|)|(^|[.?!]\s*)(\w))/gi,function(w){return w.toUpperCase();})

Neat, but does not capitalise a.m.macdonald etc.

Minkey
05-26-2008, 07:22 PM
Interesting script. Ideally though, you would have properly written text to start with - i.e. the first letter would already be uppercase.

Another nice effect is using CSS to create small caps. See the example here:

http://joeclark.org/standards/small-caps.html