/    Sign up×
Community /Pin to ProfileBookmark

Sentence Casing?

Does anyone have a JavaScript for sentence casing? Something that could convert:

>

HOW ARe you TODAY? i’m fine, THANKS.

to

>

How are you today? I’m fine, thanks.

Any help would be greatly appreciated!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJul 16.2004 — Hi!Does anyone have a JavaScript for sentence casing?[/QUOTE]Until today, I didn't have any but I botched one together for you:[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function sCase(){
val=document.getElementById('original').innerHTML;
result=new Array();
result2='';
count=0;
endSentence=new Array();
for (var i=1;i<val.length;i++){
if(val.charAt(i)=='.'||val.charAt(i)=='!'||val.charAt(i)=='?'){
endSentence[count]=val.charAt(i);
count++
}
}
var val2=val.split(/[.|?|!]/);
for (var j=0;j<val2.length;j++){
var temp=val2[j].split(' ');
if(temp.length>1){
var incr=0;
if(temp[0]==''){
incr=1;
}
temp2=temp[incr].substring(0,1);
temp3=temp[incr].substring(1,temp[incr].length);
temp2=temp2.toUpperCase();
temp3=temp3.toLowerCase();
temp[incr]=temp2+temp3;
for (var i=incr+1;i<temp.length;i++){
temp2=temp[i].substring(0,1);
temp2=temp2.toLowerCase();
temp3=temp[i].substring(1,temp[i].length);
temp3=temp3.toLowerCase();
temp[i]=temp2+temp3;
}
result2+=temp.join(' ')+endSentence[j];
}
}
document.getElementById('modified').innerHTML=result2;
}
//-->
</script>
</head>
<body>
<div id="original">HOW ARe you TODAY? i'm fine, THANKS! aND You? welL - DUNNO.</div>
<div id="modified">&nbsp;</div>
<a href="#" onclick="sCase()">Arrange cases</a>
</body>
</html>[/code]
The function will recognize full stops, question marks and exclamation marks as the end of a sentence.

Cheers - Pit

Edit: I just view the stuff in Mozilla for fun (not expecting any problems) and found, Mozilla couldn't handle the code. So I modified it to overcome that bug.
Copy linkTweet thisAlerts:
@bigjdtempauthorJul 16.2004 — THANK YOU VERY MUCH !
Copy linkTweet thisAlerts:
@PittimannJul 16.2004 — Hi!

You're welcome! :p

Cheers - Pit
×

Success!

Help @bigjdtemp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...