var s = 'test\n kuku kuku kuku kukuk \n test '; s = s.replace(/\s*\n\s*/g, '</p>\n\n<p>').replace(/(\s(?!\n|\S))+/g, ''); alert("<p>"+s+"</p>");