Home>Article>Web Front-end> How to remove spaces on both sides of a string in nodejs
In nodejs, you can use the replace method and regular expressions to remove spaces on both sides of a string. The syntax is "str.replace(/(^\s*)|(\s*$)/g, " ");" The replace() method is used to replace characters in a string, or to replace a substring that matches a regular expression.
The operating environment of this tutorial: windows10 system, nodejs version 12.19.0, DELL G3 computer.
How to remove spaces on both sides of a string in nodejs
To remove spaces on the left and right ends of a string, you can easily use trim, ltrim or rtrim, but there are no these three built-in methods in js and need to be written manually. The following implementation method uses regular expressions, which is very efficient, and adds these three methods to the built-in methods of the String object.
The method format written as a class is as follows: (str.trim();)
写成函数可以这样:(trim(str))
For more node-related knowledge, please visit:nodejs tutorial! !
The above is the detailed content of How to remove spaces on both sides of a string in nodejs. For more information, please follow other related articles on the PHP Chinese website!