Home >Web Front-end >JS Tutorial >JS enhanced version to remove spaces from strings (can remove spaces in the middle)_javascript skills

JS enhanced version to remove spaces from strings (can remove spaces in the middle)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:47:37913browse

javascript trim space removal function implementation code
http://www.jb51.net/article/16250.htm

The following is the enhanced version

Copy code The code is as follows:

// Function: 1) Remove all spaces before and after the string
// 2) Remove characters All spaces in the string (including spaces in the middle, you need to set the second parameter to: g)
function Trim(str,is_global)
{
var result;
result = str.replace(/( ^s )|(s $)/g,"");
if(is_global.toLowerCase()=="g")
result = result.replace(/s/g,"");
return result;
}
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