我在我的应用程序中使用 NicEdit 富文本编辑器(基于内容可编辑的 div),用户喜欢从 word 中粘贴。
所以我想去掉所有可能被粘贴的垃圾标签。
这是我目前正在做的事情。
//build regex to match junk tags
var unwantedtags = [ "font", "span", "table", "tbody", "div", "td", "tr", "input", "a",
"body", "button", "form", "head", "img", "select", "textarea", "tfoot", "th", "iframe", "object" ];
var unwantedregexstring= "";
$.each(unwantedtags, function(index, value) {
if(unwantedregexstring!= "") {
unwantedregexstring += "|";
}
unwantedregexstring+= "<" + value + ">";
unwantedregexstring+= "|";
unwantedregexstring+= "<" + value + "\\s[^>]*>";
unwantedregexstring+= "|";
unwantedregexstring+= "</" + value + ">";
});
var unwantedRegex = new RegExp(unwantedregexstring, "igm");
//replace junk tags with nothing
function CleanMSWordPaste(mswordtext) {
return mswordtext.replace(unwantedRegex, "");
}
//Function that gets Executed on Paste event
function ExecutePaste(){
//preserve user's selected text
var oldRng = document.selection.createRange();
//create paste area off screen and paste there
$('body').append("<div id='paster' contenteditable='true' style='height:1px;width:1px;position:fixed;left:-100px;top:-100px;'></div>");
$('#paster').focus();
$('#paster')[0].document.execCommand('paste', null, null);
//if html contains junk tags
if(unwantedRegex.test($('#paster').html())) {
//replace html with cleaned html
$('#paster').html(CleanMSWordPaste($('#paster').html()));
//select all content of paste area
var rng = document.body.createTextRange();
rng.moveToElementText($('#paster')[0]);
rng.select();
//copy cleaned html
$('#paster')[0].document.execCommand('copy', null, null);
}
//remove paste area from dom
$('#paster').remove();
//restore user's selected text
oldRng.select();
//preserves scroll position, focuses NicEditor and performs doc.execCommand('paste')
//performance of this alone is fine.
ExecCommand('paste');
}
我发现这需要很长时间(例如 word 中的 1 页文本)。我能做些什么来加快速度吗?我正在考虑某种正则表达式优化,但我对正则表达式的工作原理一无所知。