Home > Article > Web Front-end > What is the usage of javascript trim?
The javascript trim method is used to remove the leading and trailing whitespace characters of a string. Its usage syntax is "string.trim()", and the return value is a string with the leading and trailing spaces removed.
The operating environment of this article: Windows 7 system, javascript version 1.8.5, DELL G3 computer
javascript trim What is the usage?
JavaScript trim() method:
trim() method is used to remove the leading and trailing whitespace characters of a string. The whitespace characters include: Spaces, tabs, newlines and other whitespace characters.
The trim() method does not change the original string.
trim() method is not applicable to null, undefined, Number types.
Syntax
string.trim()
Return value
String, returns a string with leading and trailing spaces removed.
Example
If your browser does not support the trim() method, you can use regular expressions to achieve it:
function myTrim(x) { return x.replace(/^\s+|\s+$/gm,''); } function myFunction() { var str = myTrim(" Runoob "); alert(str); }
Output result:
Runoob
Recommended learning: "javascript basic tutorial"
The above is the detailed content of What is the usage of javascript trim?. For more information, please follow other related articles on the PHP Chinese website!