I came across the next regular expressions that I used in my final code:
// Merge multiple spaces into one space
var string = string.replace(/\s+/g,’ ‘);
// Remove space between tags
var string = string.replace(/>\s+</g,’><‘);
//Remove all kinds of line breaks.
var string = string.replace(/(\r\n|\n|\r)/gm,””)
My final code looks like:
var wikiPageTemplate = wikiPageTemplate.replace(/(\r\n|\n|\r)/gm,"").replace(/\s+/g,' ').replace(/>\s+<');
Hope you find this information useful!!