The function that can remove leading and trailing whitespace from a string at the same time is: trim(). The trim() function is used to delete the leading and trailing whitespace characters of a string. The whitespace characters include: spaces, tabs, newlines and other whitespace characters; its syntax format is "string.trim()".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.
Related recommendations: "javascript video tutorial"
JavaScript trim()
trim() method is used to delete characters The whitespace characters at the beginning and end of the string 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: Returns a string with leading and trailing spaces removed.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p>点击按钮移除字符串的头尾空格。</p> <button onclick="myFunction()">点我</button> <script> function myTrim(x) { return x.replace(/^\s+|\s+$/gm,''); } function myFunction() { var str = myTrim(" hello "); alert(str); } </script> </body> </html>
For more programming-related knowledge, please visit: Programming Learning! !
The above is the detailed content of What is the function that can remove both leading and trailing whitespace from a string?. For more information, please follow other related articles on the PHP Chinese website!