JavascriptString extension method set _ javascript skills
Source: Internet
Author: User
String extension method set, which can be javascript's string function more // get character array
String. prototype. ToCharArray = function ()
{
Return this. split ("");
}
// Obtain N identical strings
String. prototype. Repeat = function (num)
{
Var tmpArr = [];
For (var I = 0; I Return tmpArr. join ("");
}
// Reverse Order
String. prototype. Reverse = function ()
{
Return this. split (""). reverse (). join ("");
}
// Test whether it is a number.
String. prototype. IsNumeric = function ()
{
Var tmpFloat = parseFloat (this );
If (isNaN (tmpFloat) return false;
Var tmpLen = this. length-tmpFloat.toString (). length;
Return tmpFloat + "0". Repeat (tmpLen) = this;
}
// Test whether the value is an integer.
String. prototype. IsInt = function ()
{
If (this = "NaN") return false;
Return this = parseInt (this). toString ();
}
// Merge multiple blanks into one blank
String. prototype. resetBlank = function ()
{
Return this. replace (/s +/g ,"");
}
// Remove the left blank
String. prototype. LTrim = function ()
{
Return this. replace (/^ s +/g ,"");
}
// Remove the right Blank
String. prototype. RTrim = function ()
{
Return this. replace (/s + $/g ,"");
}
// Remove the blank spaces on both sides
String. prototype. trim = function ()
{
Return this. replace (/(^ s +) | (s + $)/g ,"");
}
// Retain numbers
String. prototype. getNum = function ()
{
Return this. replace (/[^ d]/g ,"");
}
// Retain letters
String. prototype. getEn = function ()
{
Return this. replace (/[^ A-Za-z]/g ,"");
}
// Retain Chinese Characters
String. prototype. getCn = function ()
{
Return this. replace (/[^ u4e00-u9fa5uf900-ufa2d]/g ,"");
}
// Get the byte length
String. prototype. getRealLength = function ()
{
Return this. replace (/[^ x00-xff]/g, "--"). length;
}
// Extract a string of the specified length from the left
String. prototype. left = function (n)
{
Return this. slice (0, n );
}
// Extract a string of the specified length from the right
String. prototype. right = function (n)
{
Return this. slice (this. length-n );
}
// HTML Encoding
String. prototype. HTMLEncode = function ()
{
Var re = this;
Var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g];
Var q2 = ["&", "<", ">", ""];
For (var I = 0; I Re = re. replace (q1 [I], q2 [I]);
Return re;
}
// Unicode Conversion
String. prototype. ascW = function ()
{
Var strText = "";
For (var I = 0; I Return strText;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.