Home > Web Front-end > Front-end Q&A > What is the usage of javascript trim?

What is the usage of javascript trim?

藏色散人
Release: 2021-10-29 12:01:13
Original
2963 people have browsed it

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()
Copy after login

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);
}
Copy after login

Output result:

Runoob
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template