Home > Web Front-end > JS Tutorial > How to remove spaces on both sides of a string using trim() in js

How to remove spaces on both sides of a string using trim() in js

伊谢尔伦
Release: 2017-05-20 10:07:54
Original
2018 people have browsed it

Removing spaces at both ends of a string in js can be achieved using regular expressions:

String.prototype.trim=function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
Copy after login

Of course the usage is very simple,

var str =" Hi,My Name is Water! ";
str.trim();
Copy after login

Of course you can also write a function yourself, or some people are used to this:

function trim(stri) { return stri.replace(/(^\s*)|(\s*$)/g, ""); }
var str =" Hi,My Name is Water! ";
trim(str);
Copy after login

【Related recommendations 】

1. Share examples of TrimHelper method in Java

2. Detailed explanation of String.trim() method examples in Java

3. Detailed explanation of usage examples of java trim

4. In Java trim() function

5. The PHP function trim() that removes leading and trailing spaces and special characters from a string

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