好吧,這裏是我終於想出了。另外一個可以添加的東西是將所有HTML實體(lt;>
等)替換爲它們的等價值。它可能會刪除更多的換行符,直接從屏幕上覆制它,但這是我所需要的。
$.fn.getDelimitedText = function(){
var pres = [];
var str = '';
var checkSpace = /[ \r\n]+/gmi;
var checkTags = /<(?!pre[ >])([^ \/>]*)(?:"[^"]*?"|[^">])*>/gmi;
var checkLineBreaks = /\r?\n\r?\n/gmi;
//Remove all comments immediately so they don't interfere.
//Use alternation to catch line breaks also.
var test = $(this).html().replace(/<!--(?:.|\n)*?-->/gmi, '');
var count = 0;
//replace all pre elements with a placeholder and store the contents
//in an array so they don't get modified.
test.replace(/<(pre)(?: (?:"[^"]*?"|[^">])*)?>([\s\S]*?)<\/\1>/gmi,
function(match, tag, body, offset, input){
pres.push(body);
return '<pre ' + (pres.length - 1) + '/>';
});
function doTableRegex(outer, index){
var lastPosition = 0;
var table = {};
//tbody, thead, tfoot. Don't assign the return to anything. We get that from the table var.
outer.replace(/<(tbody|thead|tfoot)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, function(match, tag, body){
//replace td and th elements
body = body.replace(/<(th|td)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, function(match, tag, body1){
return body1.replace(checkTags, '').replace(checkSpace, ' ') + "\t";
});
//replace tr elements
body = body.replace(/<(tr)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, function(match, tag, body1){
return body1.replace(checkTags, '').replace(checkSpace, ' ') + "\r\n";
});
//Multiple tbody elements are allowed in a table.
//We'll allow for multiples of everything.
if(table[tag]) table[tag] += "\r\n" + body;
else table[tag] = body;
//return nothing.
return '';
});
//put the table sections in the correct order.
return (table.thead || "") + "\r\n" + (table.tbody || "") + "\r\n" + (table.tfoot || "") + "\r\n";
}
//Replace all tables
return test.replace(/<(table)(?: (?:"[^"]*"|[^">])*)?>([\s\S]*?)<\/\1>/gmi, doTableRegex)
//replace br & hr tags with line breaks
.replace(/<br ?\/?>|<hr ?\/?>/g, "\r\n")
//remove all tags besides pre tags
.replace(checkTags, '')
//empty all lines of spaces
.replace(/^ +$/gmi, '')
//remove all empty lines
.replace(/(?:\r?\n){2,}/gmi, "\r\n")
//replace all double (and more) spaces with single spaces.
.replace(/ +/gmi, ' ')
//Add the contents of pre elements back in.
.replace(/<pre(?: (?:id="([^"])"|"[^"]*"|[^">])*)?>/gmi, function(match, id){
return "\r\n" + pres[parseInt(id)] + "\r\n";
});
};
我們可以得到一個JSFiddle代碼來更好地理解您的問題嗎? –
$ myTable.html()? –
'.text()'和'.html()'都不會做你想要的。您必須編寫自己的代碼來遍歷錶行,以便爲每個表生成一個製表符分隔的表示形式。 – Pointy