js统计字数代码
js实现统计字数,js统计字数代码教程如下:
输入一段文字
实现结果:统计出汉字、符号数量
js实现代码如下:
var contentObj = $('#content');
contentObj.bind('input propertychange change',
function() {
count();
});
// 涓€閿粺璁�
function count() {
var content = contentObj.val().replace(/\r\n/g, "\n"); // 瀹屾暣鐨勫唴瀹�
var str = content.replace(/\n/g, ''); // 绾补瀛楃
var Chinese_characters = content.match(/[\u4e00-\u9fa5]/g) || []; // 涓枃瀛楃
var phrase = content.match(/\b\w+\b/g) || []; // 鏁板瓧+瀛楁瘝
var group_number = content.match(/\b\d+\b/g) || []; // 鏁板瓧
var letter = str.match(/[A-Za-z]/g) || []; // 鑻辨枃瀛楁瘝
var number = str.match(/[0-9]/g) || []; // 鏁板瓧
// 鑻辨枃鏍囩偣
var half_punctuation = str.match(/[|\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g) || [];
// 涓枃瀛楃鎬绘暟
var Chinese_all = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
if (c.match(/[^\x00-\xff]/)) Chinese_all++;
}
// 璁$畻娈佃惤鏁�
var part = 0;
var s_ma = content.split("\n");
for (var i = 0; i < s_ma.length; i++) {
if (s_ma[i].length > 0) part++;
}
// 瀛楃鎬绘暟
$('#id_total').html(str.length);
// 姹夊瓧鏁�
$('#id_c_total').html(Chinese_characters.length);
// 涓枃鏍囩偣
$('#id_c_punctuation').html(Chinese_all - Chinese_characters.length);
// 鑻辨枃瀛楁暟
$('#id_e_total').html(letter.length);
// 鑻辨枃鏍囩偣
$('#id_e_punctuation').html(half_punctuation.length);
// 鑻辨枃鍗曡瘝
$('#id_e_words').html(phrase.length - group_number.length);
// 鏁板瓧鍗曡瘝
$('#id_n_words').html(group_number.length);
// 鏁板瓧瀛楃
$('#id_n_total').html(number.length);
// 琛屾暟
$('#id_part').html(part);
}
// 娓呴櫎琛屽熬绌烘牸
function noSpace() {
var str = contentObj.val().replace(/\r\n/g, "\n").replace(/\n/g, "[mk~鎹㈣]");
var m = str.split("[mk~鎹㈣]");
var ma = [];
var len = m.length;
for (var i = 0; i < len; i++) {
ma.push(m[i].replace(/(\s*$)/g, ""));
}
contentObj.val(ma.join("\r\n"));
count(); // 閲嶆柊缁熻瀛楁暟
}
// 涓€閿帓鐗�
function format() {
var str = contentObj.val().replace(/[\r\n]+/g, "[mk~鎹㈣]").replace(/[\n\n]+/g, "[mk~鎹㈣]").replace(/[\n]+/g, "[mk~鎹㈣]");
var m = str.split("[mk~鎹㈣]");
var ma = [];
var len = m.length;
for (var i = 0; i < len; i++) {
ma.push('銆€銆€' + m[i].replace(/(^\s*)|(\s*$)/g, "")); // 缂╄繘锛屽幓琛屽熬绌烘牸
}
contentObj.val(ma.join("\r\n\r\n")); // 娈佃惤鎹㈣
count(); // 閲嶆柊缁熻瀛楁暟
}
// 鍒犻櫎绌鸿
function noEmptyLines() {
var str = contentObj.val().replace(/[\r\n]+/g, "[mk~鎹㈣]").replace(/[\n\n]+/g, "[mk~鎹㈣]").replace(/[\n]+/g, "[mk~鎹㈣]");
var m = str.split("[mk~鎹㈣]");
contentObj.val(m.join("\r\n")); // 娈佃惤鎹㈣
count(); // 閲嶆柊缁熻瀛楁暟
}
// 鍗婅杞叏瑙�
function format4() {
var body = document.getElementById("thebody").value;
for (var ii = 0; 100 > ii; ii++) {
body = body.replace("銆€", ""); //鍘婚櫎鍏ㄨ绌烘牸
body = body.replace(",", "锛�"); //鏇挎崲鑻辨枃鏍囩偣
body = body.replace("......", "鈥︹€�");
body = body.replace("銆傘€傘€傘€傘€傘€�", "鈥︹€�");
body = body.replace("?", "锛�");
body = body.replace(".", "銆�");
body = body.replace(";", "锛�");
body = body.replace(":", "锛�");
body = body.replace("!", "锛�");
body = body.replace("(", "锛�");
body = body.replace(")", "锛�");
body = body.replace("----", "鈥斺€�");
body = body.replace("--", "鈥斺€�");
body = body.replace("[", "锛�");
body = body.replace("]", "锛�");
}
document.getElementById("thebody").value = body;
}
显示效果:字数统计器
参考文章:http://tishang.net/articles/1685614832.html