Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 956
Anil Kumar Pandey Avatar asked Oct 28 '13 07:10

Anil Kumar Pandey


People also ask

Does trim remove line breaks?

trim method removes any line breaks from the start and end of a string. It handles all line terminator characters (LF, CR, etc). The method also removes any leading or trailing spaces or tabs. The trim() method doesn't change the original string, it returns a new string.

Which of the following method is used to remove a new line from the right end if there is one?

The slice and stitch method: It is the basic way to realize the solution to this problem. Visit each character of the string and slice them in such a way that it removes the newline and carriage return characters.

What is trim function jQuery?

jQuery trim() method The trim() method is used to remove the space, tabs, and all line breaks from the starting and end of the specified string. This method does not remove these characters if these whitespace characters are in the middle of the string. The commonly used syntax of using this method is given as follows.

How do I remove the first character in jQuery?

Answer: Use the JavaScript substring() method You can use the JavaScript substring() method to remove first character form a string. A typical example is removing hash ( # ) character from fragment identifier.


1 Answers

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

reference replace

\s matches any newline or tab or white-space

like image 195
Rituraj ratan Avatar answered Oct 15 '22 08:10

Rituraj ratan