您的位置:首页 > 产品设计 > UI/UE

Ueditor更改统计字数与内容保存去除字符实体

2018-02-10 17:32 246 查看
Ueditor 默认统计方式为统计字符,今天项目中要求输入英文写作,所以要对单词数进行统计。直接上代码

<script type="text/javascript">
//初始化Ueditor
var ue = UE.getEditor('container', {
toolbars: [
['undo', 'redo', 'bold', 'italic', 'underline','rowspacingtop','rowspacingbottom','lineheight','fontfamily','fontsize','justifyleft','justifycenter','justifyright','justifyjustify','fullscreen']
],
autoHeightEnabled: false,
autoFloatEnabled: true,
elementPathEnabled:false,
wordCount:false,//*这是重点 关闭自带的字数统计
});
//当Ueditor加载完成后,显示统计字符的div并更改内容
ue.addListener( 'ready', function( editor ) {
//edui1_wordcount div为字符统计的显示区域,关闭字符统计后 display=none 所以要显示
$("#edui1_wordcount").show();
$("#edui1_wordcount").html("单词统计");
} );
//为Ueditor添加键盘监听事件,每次敲击后统计单词数
ue.addListener('keydown',function()count(ue.getContentTxt())});
/* 单词统计 */
function count(str){
var value=str;
//标点转换为空格
value=value.replace(/[\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g," ");
//统计中文个数
var chinese=value.match(/[\u4e00-\u9fa5]/g);
//去除中文
value = value.replace(/[\u4e00-\u9fa5]+/g, " ");
// 将换行符,前后空格替换为空格
value = value.replace(/\n|\r|^\s+|\s+$/gi," ");
// 多个空格替换成一个空格
value = value.replace(/\s+/gi," ");
// 更新计数
var length = 0;
var match = value.match(/\s/g);
if (match) {
length = match.length + 1;
} else if (value) {
length = 1;
}
length = length+(chinese?chinese.length:0);
$("#edui1_wordcount").html("您总共输入了"+length+"个单词");
}
/* 单词统计 */
</script>


统计单词部分写的不够完善,请自行忽略

下面是java对Ueditor富文本的处理

主要两部分:

1.去除html标签,百度上一大堆。

2.处理html中的字符实体例如:

 >
重点记录    ​(tab按键)


可以使用

org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(html);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐