Home > Web Front-end > Front-end Q&A > How to remove leading and trailing spaces in jquery

How to remove leading and trailing spaces in jquery

青灯夜游
Release: 2022-09-13 17:38:31
Original
2278 people have browsed it

Two implementation methods: 1. Use the trim() function to remove all whitespace characters (newlines, spaces, and tabs) at the beginning and end of the string. The syntax "$.trim(specified characters string)". 2. Use the replace() function with regular expressions to replace leading and trailing spaces with null characters. The syntax is "specify string object.replace(/^\s |\s $/g, "")".

How to remove leading and trailing spaces in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

Method 1: Use the trim() function to remove

$.trim() function is used to remove whitespace characters at both ends of the string.

$.trim( str )
Copy after login

Note: The $.trim() function will remove all newline characters, spaces (including consecutive spaces) and tab characters at the beginning and end of the string. If these whitespace characters are in the middle of the string, they are retained and not removed.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-3.6.1.min.js"></script>
		<script type="text/javascript">
			$(function() {
				var str = "         lots of spaces before and after         ";
				console.log(str);
				console.log($.trim(str));
			})
		</script>
	</head>
	<body>
	</body>
</html>
Copy after login

How to remove leading and trailing spaces in jquery

Method 2: Use the replace() function with regular expressions to remove

replace() function is used in strings Replace some characters with other characters, or replace a substring that matches a regular expression.

stringObject.replace(regexp/substr,replacement)
Copy after login
ParametersDescription
regexp/substr

Required. A RegExp object that specifies the substring or pattern to be replaced.

Note that if the value is a string, it is retrieved as a literal text pattern, rather than first being converted to a RegExp object.

replacementRequired. A string value. Specifies functions for replacing text or generating replacement text.

Just use regular expressions to match leading and trailing spaces and replace them with null characters.

Regular expression used: /^\s |\s $/g

Syntax:

str.replace(/^\s+|\s+$/g, "");
Copy after login
  • // are regular expressions, and the following g indicates global matching

  • ^\s Remove leading space. ^ means the beginning, \s is a space, matches 1 or more characters, so what it means here is to match 1 or more spaces at the beginning

  • \s $Remove trailing spaces. $ indicates the end of the string, indicating matching of one or more spaces at the end.

  • #| in the middle indicates matching the regular pattern on the left or right , so as long as a string has spaces at the beginning or end, the space part will be matched to

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-3.6.1.min.js"></script>
		<script type="text/javascript">
			$(function() {
				var str = "         lots of spaces before and after         ";
				console.log(str);
				console.log(str.replace(/^\s+|\s+$/g, ""));
			})
		</script>
	</head>
	<body>
	</body>
</html>
Copy after login

How to remove leading and trailing spaces in jquery

[Recommended learning: jQuery video tutorial, web front-end video]

The above is the detailed content of How to remove leading and trailing spaces in jquery. 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