Home >Web Front-end >JS Tutorial >JS uses regular expressions to remove spaces on the left and right sides of a string
The example in this article describes how JS uses regular expressions to remove spaces on the left and right sides of strings. Share it with everyone for your reference, the details are as follows:
//去掉左空格 function ltrim(s) { return s.replace(/^/s*/, ""); } //去掉右空格 function rtrim(s) { return s.replace(//s*$/, ""); } //去掉左右空格 function trim(s) { return rtrim(ltrim(s)); }
I hope this article will be helpful to everyone in JavaScript programming.
For more related articles on how to use regular JS to remove spaces on the left and right sides of strings, please pay attention to the PHP Chinese website!