Home  >  Article  >  Web Front-end  >  What is the usage of javascript trim?

What is the usage of javascript trim?

藏色散人
藏色散人Original
2021-10-29 12:01:132880browse

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.

What is the usage of javascript trim?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn