We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Need Sentence Case not ProperCase - Please

bburden
bburden asked
on
Medium Priority
2,253 Views
Last Modified: 2012-05-05
I need a javascript that does Sentence Case as in MS Word.  Title or ProperCase caps the first letter of every word.  exp.. "This Is The Day.." - Sentence Case is "This is the day."

I have search in many forum and can not find wihat I am looking for.  I have found some samples you can put in Access of the after update, and by putting  a public function in a module. ProperCase Not Sentence Case.

I have found some examples as the one below to use in javascript.  This is the one I pefer.

<script language="javascript">
function firstLetterUpper(fml) {
    var pattern = /(\w)(\w*)/;
    var a = fml.value.split(/\s+/g);
    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern);
        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();
             a[i] = firstLetter + restOfWord;
    }
    fml.value = a.join(' ');
}
</script>

I just need it to just cap the first letter in the sentence and then after the peroid or question mark cap the first letter of the next sentence.

I am sure this can be done, but I can't seem to fiqure it out.

Thank you in advance.
Comment
Watch Question

function capitalize(str) {
  var re = /([?\.\!])( *)(\w)/g;
      str = str.replace(re, function(x){return RegExp.$1 + RegExp.$2 + RegExp.$3.toUpperCase()});
      return str.replace(/^./,function(x){return x.toUpperCase()}); //Capitalize first char, inelegantly.
      
}

Capitalizes any alphanumeric after ? or . or ! followed by any amount of spaces.

Very similar (in fact, a modified function) to http:Q_21779941.html
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Simmelar, but still different:

<script>

var theText = "This Is The first Day. This Is The second Day. This Is The third Day.";

theText = theText.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});

document.write(theText);
</script>

Author

Commented:
Hello all and thanks.  I guess I did not explain myself clearly, I am bad about that.  I need to associate the above javascript with a text box or text area.  If the text area is filled in incorrectly by the user, for example they type in all CAPS  I want it to convert to Sentence Case as the example I have listed.  I do not want every letter to be cap of each word.

Maybe I am just not getting what you guys are saying please forgive my ingorance.

<script language="javascript">
function firstLetterUpper(fml) {
    var pattern = /(\w)(\w*)/;
    var a = fml.value.split(/\s+/g);
    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern);
        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();
             a[i] = firstLetter + restOfWord;
    }
    fml.value = a.join(' ');
}
</script>

</head>

<body>
<form>
<b>
<p><input type="text" name="fml" size="30" onChange="return firstLetterUpper(this);"></p>
</b>
  <textarea name="textfield" cols="50" rows="4" onChange="return firstLetterUpper(this);"></textarea>

<b>
<p><input type="submit" value="Submit" name="B1" ID="Submit1">
</b>

Thanks again for all your help.
Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
And I guess you did not test my version:

function firstLetterUpper(fml) {
  fml.value = fml.value.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Hi bburden,

Zvonko provided a top notch solution once again.  He provides a snippet to show how and/or that it works, then he's on to help 100s of other people.  You have working code in your example, and so you just need to use his solution in the same manner...

function sentenceCase(aForm) {
    var theText = aForm.value;
    theText = theText.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});
    aForm.value = theText;
}

You can call that function the same as you called your proper case function.  Sweet.  I'd give him an A!

mvan

Commented:
Look at that, he was back to show how it fit into your program ... quicker than I could type my message!

Thanks for sharing your wisdom, Zvonko.

mvan

Author

Commented:
I was never doubting Zvonko , I said it was just me not getting it.  That why I said in advance forgive for my ingornace.  I have given him a A grade and accepted his answer.  

I am not astute as most in here, but I do know how to appreciate help that is given.

Thanks again Zvonko

Commented:
Sorry, I didn't mean to call your gratitude or appreciation into question.  I just meant that my putting it into a function wouldn't merit any me any credit - he did the real work ... and while I was typing my message, he went ahead and put it into a function too (I didn't see that before I hit the submit on mine).

mvan

Author

Commented:
All is well, Mvan, Everthing is Everthing. I do appreciate all you guys in here.  To everyone have a great week.

Later until the next time.
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
You are welcome.
And please consider splitting points to all experts who invested time and efforts to code the solution AND to explain the solution in plain English words to help you understand.

Thanks mvan.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.