Make first character uppercase but not after specific word

I am writing JavaScript code. The aim is to make every first character of a word uppercase after full-stop character (Hello world. Hi) while typing in textarea.

For that I’m using following code …

$('#div2').on('input', function (evt) {
    var re = /(^|[.!?]\s+)([a-z])/g;
    var box = evt.target;
    var stringStart = box.selectionStart;
    var stringEnd = box.selectionEnd; 
    var val = $(evt.target).val().replace(re, function (m, $1, $2) {
        return $1 + $2.toUpperCase()
    });

    $(evt.target).val(val);
    box.setSelectionRange(stringStart, stringEnd); 
});

Which works as expected.
But now i want it should skip some word like (U.S.A.). if one typed the the word U.S.A. in textare the first character of next word should not be uppercase.

E.g. 

    U.S.A. is the //Expected  
    U.S.A. Is the //what i am getting (wrong) 

To achive this i wrote below code, which not working as expected.

var skipWordUpper = ['U.S.A.', 'Inc.'];
$('#div2').on('input', function (evt) {
    var re = /(^|[.!?]\s+)([a-z])/g;
    var box = evt.target;
    var stringStart = box.selectionStart;
    var stringEnd = box.selectionEnd;

    var str = $('#div2').val();
    var beforeSpace = str.split(" ").splice(-2) 
    var foundPresent = $.inArray(beforeSpace[0], skipWordUpper) > -1; 

    if (!foundPresent) {
        var val = $(evt.target).val().replace(re, function (m, $1, $2) {
            return $1 + $2.toUpperCase()
        });

        $(evt.target).val(val);
        box.setSelectionRange(stringStart, stringEnd);
    } 
});

Anybody will plz help to find out what mistake i am making and put me in proper way.
or any changes in regular exp. is required.
Apology for my hellybelly knowledge of JAVASCRIPT

14 thoughts on “Make first character uppercase but not after specific word”

Leave a Comment