简体   繁体   中英

How can I auto capitalize the first word of a sentence using Ckeditor?

While typing inside CKEditor/Textbox automatically Every first letter of sentence should be in upper case. In multiline text box its working well. But CKeditor its not working. The code I have tried is below.

var applySentenceCase = function(e) {
    this.value = this.value.replace(/.+?[\.\?\!](\s|$)/g, function(letter) {
        return letter.charAt(0).toUpperCase() + letter.substr(1).toLowerCase();
    }); 
}       
var capitalize = function(e) {
    if (this.value.match(/^[a-z]/)) {
        this.value = this.value.replace(/^./, function(letter) {
            return letter.toUpperCase();
        });
    }
}    
document.getElementById('TextBox1').addEventListener('keyup', capitalize);
document.getElementById('TextBox1').addEventListener('mouseup', capitalize);
document.getElementById('TextBox1').addEventListener('keyup', applySentenceCase);
document.getElementById('TextBox1').addEventListener('mouseup', applySentenceCase);

For CKEditor


             $(document).ready(function(){
                       var applySentenceCase = function (e) {
                           this.value = this.value.replace(/.+?[\.\?\!](\s|$)/g, function (letter) {
                               return letter.charAt(0).toUpperCase() + letter.substr(1).toLowerCase();
                           });
                       }

             });
             $(document).ready(function () {
                       var capitalize = function (e) {
                           if (this.value.match(/^[a-z]/)) {
                               this.value = this.value.replace(/^./, function (letter) {
                                   return letter.toUpperCase();
                               });
                           }
                       }
             });

                       document.getElementById('CKEditor1').addEventListener('keyup', capitalize);
                       document.getElementById('CKEditor1').addEventListener('mouseup', capitalize);
                       document.getElementById('CKEditor1').addEventListener('keyup', applySentenceCase);
                       document.getElementById('CKEditor1').addEventListener('mouseup', applySentenceCase);

        </script>
    </div>

You can do this with css also

p.caps:first-letter{
  text-transform: capitalize
}

here is fiddle

Try like this

var str = "hello world";
str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});
alert(str); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM