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

高洛峰
高洛峰Original
2017-01-09 15:44:401246browse

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!


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