I’m no regex guru, so I split this into 4 separate pieces:
// remove newline / carriage return str.replace(/\n/g, ""); // remove whitespace (space and tabs) before tags str.replace(/[\t ]+\</g, "<"); // remove whitespace between tags str.replace(/\>[\t ]+\</g, "><"); // remove whitespace after tags str.replace(/\>[\t ]+$/g, ">");
Return the replacement string and set it to the value you want to save. I wanted to use jQuery, so here’s the final product:
$("#copy-txt").val( $("#nn-results").html().replace(/\n/g, "") .replace(/[\t ]+\</g, "<") .replace(/\>[\t ]+\</g, "><") .replace(/\>[\t ]+$/g, ">") );
I have a containing div with the id “nn-results”, grab it’s inner html, do the replacements on that html, and then set the id=”copy-txt” input element value to be the results. I have seen some posts where people tried to make all that replacement happen in one regex, but I wasn’t good enough with the regex/js combo to make it work. If someone is ready to go pro, and it isn’t easy, please comment below.