我想將JavaScript HTML轉換器轉換爲文本。我所做的一切都很好,但我無法處理鏈接。我需要的reg表達式是使文本版本的環節用於電子郵件模板的HTML到文本轉換器(javascript)
HTML版本:
<a href="http://link.com">Link text</a>
轉換鏈接文字版:
Link text(http://link.com)
我的代碼
$('body').on('click','[data-action="convertTemplateToText"]', function() {
var html = $("#clientHTML").val();
if (html) {
html = html.replace(/<!doctype.*>/i,"");
html = html.replace(/<head>[\w\W]*?<\/head>/i,"");
html = html.replace(/<style.*>[\w\W]*?<\/style>/gi,"");
html = html.replace(/<script.*>[\w\W]*?<\/script>/gi,"");
html = html.replace(/\n|\r/g," ");
html = html.replace(/\<\/p\>/gi,"\n");
html = html.replace(/\<\/li\>/gi," ");
html = html.replace(/\<br\s*?\/?\>/gi,"\n");
html = strip_tags(html,'<a>');
html = html_entity_decode(html,'HTML_ENTITIES');
html = html.replace(/([ \t])+/g," ");
html = html.replace(/\n /g,"\n");
if (html.charAt(0) == ' ') {
html = html.substr(1);
}
} else {
html = '';
}
$("#clientText").val(html);
$('#templateTextContainer').slideDown(500);
return false;
});
幫助我請
nope ...我需要註冊。我用textarea獲得了價值,在處理完文本之後,我應該替換所有鏈接,但是謝謝你的回答 –
我已經用例子添加了一個工作解決方案。 – wilku
http://regexr.com/38qf9 - 請檢查此鏈接。如果鏈接在一行中,然後reg exp是不正確的:( –