javascript replace regex spaces and newlines

how to replace spaces and newlines together with additional closing code ; in javascript?

function main(){
   var a = 'hello'
   console.log(a)
}

console.log(main.toString().replace(/[n ]/g,''))

output

functionmain(){vara='hello'console.log(a)}

I want

function main(){var a='hello';console.log(a);}