Home > Web Front-end > JS Tutorial > JS uses regular expressions to remove spaces on the left and right sides of a string

JS uses regular expressions to remove spaces on the left and right sides of a string

高洛峰
Release: 2017-01-09 15:44:40
Original
1251 people have browsed it

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));
}
Copy after login

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!


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