JS TRIM (), LTRIM (), RTRIM () function

xiaoxiao2021-03-06  22

Add TRIM, LTRIM, RTRIM Method for String Objects in JavaScript

Using the Prototype property of each object in JavaScript We can add our own methods and properties to the built-in objects in JavaScript.

Here we use this property to add three methods for String objects: TRIM, LTRIM, RTRIM (the same function as the same name in VBScript)

String.prototype.trim = function ()

{

Return this.Replace (/ (^ / s *) | (/ s * $) / g, "");

}

String.Prototype.ltrim = function ()

{

Return this.Replace (/ (^ / s *) / g, "");

}

String.prototype.rtrim = function ()

{

Return this.Replace (/ S * $) / g, "");

}

How, simple, look at an example of use: