Home > Web Front-end > JS Tutorial > What is the function that can remove both leading and trailing whitespace from a string?

What is the function that can remove both leading and trailing whitespace from a string?

青灯夜游
Release: 2022-12-30 11:13:04
Original
5695 people have browsed it

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()".

What is the function that can remove both leading and trailing whitespace from a string?

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

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,&#39;&#39;);
}

function myFunction() {
  var str = myTrim("    hello    ");
  alert(str);
}
</script>

</body>
</html>
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template