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.
str.replace(/\s+/g, " "); //try this
reference replace
\\s matches any newline or tab or white-space
You can do this:
var str = "Welcome\n\tTo\n\nBeautiful\t\t\t\nWorld";
alert($.trim(str.replace(/[\t\n]+/g,' ')));
// results is "Welcome To Beautiful World"
That is expected. trim
only takes care of leading and trailing whitespace.
Instead, use
str.split(/\s/).join(' ');
In your example, this returns
"Welcome To Beautiful World"
You can use replace with a regular expression :
var str = "Welcome\n\tTo\n\nBeautiful\t\t\t\nWorld";
alert($.trim(str.replace(/[\t\n]+/g, ' ')));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.