Search code examples
javascriptace-editor

ACE editor count number of characters?


This is how I am getting the total number of lines in ACE editor...

editor.getSession().on('change', function(){
    var lines = editor.session.getLength();
    $('#lines').empty().append(lines);
});

This works fine, but how can I get the total characters also? I can't find any information on this in the API documentation.

Thanks.

EDIT...

This is how I ended up doing this... it also divides the output with a thousands separator.

var lines = setting.editor.getLength().toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
var chars = setting.editor.getValue().length.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
$('#lines').empty().append(lines);
$('#chars').empty().append(chars);

Solution

  • Try to use this:

    console.log(editor.session.getValue().length);