Wikipedia:AutoEd/htmltowikitext.js#L-18
//Convert HTML to wikitext
function autoEdHTMLtoWikitext(str) {
// and tags
str = str.replace(/((?:[^<>]|<[a-z][^<>]*\/>|<([a-z]+)(?:| [^<>]*)>[^<>]*<\/\3>)*?)<\/\1[ ]*>/gi, "$1");
str = str.replace(/((?:[^<>]|<[a-z][^<>]*\/>|<([a-z]+)(?:| [^<>]*)>[^<>]*<\/\3>)*?)<\/\1[ ]*>/gi, "$1");
// , <\br>,
,
, ...
str = str.replace(/<[\\\/]+BR[\\\/\s]*>/gim, '
');
str = str.replace(/<[\\\/\s]*BR[\s]*[\\\/]+[\s]*>/gim, '
');
// <.br>,
, ...
str = str.replace(/<[\s\.]*BR[\s\.]*>/gim, '
');
//
>, <
, <
> ...
str = str.replace(/<[\s]*(
)/gim, '$1');
str = str.replace(/(
)[\s]*>/gim, '$1');
//
str = str.replace(/([\r\n])[\t ]*<[\\\/\. ]*HR[\\\/\. ]*>/gi, '$1----');
// str = str.replace(/(.)<[\\\/\. ]*HR[\\\/\. ]*>/gi, '$1\n----'); // Breaks wikitables
// Repeated references tag
str = str.replace(/(
// Make sure
, ..., is after a newline
str = str.replace(/([^\r\n ])[\t ]*(
// Make sure , ..., is before a newline
str = str.replace(/(<\/H[1-6][^<>]*>)[\t ]*([^\r\n ])/gim, '$1\n$2');
// Remove newlines from inside
, ...,
var loopcount = 0;
while( str.search( /
str = str.replace(/(
loopcount++;
}
// Replace
, ..., with wikified section headings
str = str.replace(/(^|[\r\n])[\t ]*
]*>([^\r\n]*?)<\/H1[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1=$2=$3');
str = str.replace(/(^|[\r\n])[\t ]*
]*>([^\r\n]*?)<\/H2[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1==$2==$3');
str = str.replace(/(^|[\r\n])[\t ]*
]*>([^\r\n]*?)<\/H3[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1===$2===$3');
str = str.replace(/(^|[\r\n])[\t ]*
]*>([^\r\n]*?)<\/H4[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1====$2====$3');
str = str.replace(/(^|[\r\n])[\t ]*
]*>([^\r\n]*?)<\/H5[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1=====$2=====$3');
str = str.replace(/(^|[\r\n])[\t ]*
]*>([^\r\n]*?)<\/H6[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1======$2======$3');
return str;
}