Code
The code is as follows |
Copy Code |
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 left space function String.prototype.Rtrim () {return this.replace (/(/s*$)/g, "");} Remove Right space |
Cases
The code is as follows |
Copy Code |
<script type= "Text/javascript" > function Trim (str) {//Remove spaces at left and right ends Return Str.replace (/(^s*) | ( s*$)/g, ""); } function LTrim (str) {//delete left space Return Str.replace (/(^s*)/g, ""); } function RTrim (str) {//Remove the right space Return Str.replace (/(s*$)/g, ""); } </script> |
example, remove all spaces
Program code
code is as follows |
copy code |
<script Language= "JavaScript" <!-- //provenance: Online collection //made by Yaosansi 2005-12-02 //for more visit http:// Www.yaosansi.com //Trim (), Ltrim (), RTrim () String.prototype.Trim = function () { Return This.repla CE (/(^s*) | ( s*$)/g, ""); } String.prototype.LTrim = function () { return This.replace (/(^s*)/g, ""); } String.prototype.RTrim = function () { return This.replace (/(s*$)/g, ""); } //--> </script> |
Let's take a look at what "s/s represents in the JS script.
Reference content
s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV].
Please bear in mind that the lowercase s