Home>Article>Web Front-end> 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

伊谢尔伦
伊谢尔伦 Original
2017-05-20 10:07:54 1938browse

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, ""); }

Of course the usage is very simple,

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

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);

【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

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