Home  >  Article  >  Web Front-end  >  Examples of removing leading and trailing spaces in javascript_javascript skills

Examples of removing leading and trailing spaces in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:16:53934browse

Code
Copy code The code is as follows:

function String.prototype.Trim() { return this.replace(/(^/s*)|(/s*$)/g, ""); } // Remove left and right spaces
function String.prototype.Ltrim() { return this.replace( /(^/s*)/g, ""); }                                                                                                                                                                                                                          // Remove the left space
function String.prototype.Rtrim() { return this.replace(/(/s*$)/g, "") ;} // Remove the right space

Example
Copy code The code is as follows: 🎜>



Let’s take a look at what “/s means” in Js scripts.
s matches any whitespace character, including Spaces, tabs, form feeds, etc. Equivalent to [fnrtv].
Please remember that it is lowercase s
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
Previous article:Three ways to dynamically load JS files_javascript skillsNext article:Three ways to dynamically load JS files_javascript skills

Related articles

See more