【发布时间】:2016-12-18 13:18:42
【问题描述】:
我在 tinymce 之外有一个字数 div,它显示字数,但不使用 wordCount 插件,而是使用正则表达式来计算字数。
但是当我添加项目符号或对已键入的文本应用粗体时,此计数未显示正确的值[它显示计数为 3,而我在使用项目符号时仅输入了一个单词并将计数增加 2,而突出显示已输入的文本]
当使用粗体或斜体、下划线或项目符号或使用 wordCount 插件在状态栏外使用它的输出时,任何人都可以建议在正则表达式中做什么以获得正确的计数[在这种情况下,在我的字数统计 div 中]
代码如下:
tinymceConfig = {
mode:"exact",
elements:"essay",
menubar: false,
statusbar: false,
plugins: "autoresize",
content_css : '../../theme/css/Language/editor.css',
toolbar : "bold italic underline bullist",
resize:"height",
autoresize_max_height: 325,
setup : function(editor) {
if ($('#essay').prop('readonly')) {
editor.settings.readonly = true;
}
editor.on('keydown', function (evt) {
var wordCount = 0;
var valid_keys = [8, 46];
text = editor.getContent().replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' ');
text = text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
wordCount = text.split(' ').length-1;
if(wordCount >= Helpers.constants.MAX_WORDS && valid_keys.indexOf(evt.keyCode) == -1)
{
evt.preventDefault();
Helpers.prompt('You have reached the maximum word limit.');
//evt.stopPropagation();
return false;
}
});
editor.on('keyup', function (evt) {
var text = '';
clearTimeout(saveEssayIntervalId);
saveEssayIntervalId = setTimeout(function() {
saveEssay('silent');
}, 3000);
text = editor.getContent().replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' ');
text = text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var wordCount = text.split(' ').length;
$("#essayContainer .textAreaAfter").html("[ Words entered: "+wordCount+" ]");
});
} };
tinyMCE.init(tinymceConfig);
【问题讨论】:
标签: javascript regex tinymce word-count