var str = '\t\t\t\sdfsdt\nf\nsdf { sdf\t \n } sdf\n\t } { \n\t '; str = str.replace(/\s+/g, ' ').replace(/\s*({|})\s*/g, '$1'); alert('[' + str + ']');