Javascript – jQuery: How to trim new line and tab characters between words

javascriptjquery

Hi I am getting new line(\n) and tab(\t) characters between words and I was trying to trim those by using $.trim() function but its not working. So can anyone have some solution for this type of problem.

Ex:

var str = "Welcome\n\tTo\n\nBeautiful\t\t\t\nWorld";
alert($.trim(str));

the above code is not working.

Best Answer

str.replace(/\s+/g, " "); //try this

reference replace

\s matches any newline or tab or white-space