Some javascript Functions implemented by String. prototype _ javascript skills

Source: Internet
Author: User
This article mainly introduces some javascript Functions implemented by String. prototype in detail. If you need them, please refer to them and hope to help you. The Code is as follows:


// String. prototype

// Batch replace, such as str. replaceAll ([/a/g,/B/g,/c/g], ["aaa", "bbb", "ccc"])
String. prototype. ReplaceAll = function (A, B ){
Var C = this;
For (var I = 0; I C = C. replace (A [I], B [I]);
};
Return C;
};

// Remove the white spaces at both ends of the character
String. prototype. Trim = function (){
Return this. replace (/(^ [/t/n/r] *) | ([/t/n/r] * $)/g ,'');
};

// Remove the white space on the left of the character
String. prototype. LTrim = function (){
Return this. replace (/^ [/t/n/r]/g ,'');
};

// Remove the white space on the right of the character
String. prototype. RTrim = function (){
Return this. replace (/[/t/n/r] * $/g ,'');
};

// Returns the length of two characters in a Chinese character.
String. prototype. ChineseLength = function ()
{
Return this. replace (/[^/x00-/xff]/g, "**"). length;
};

// Determine whether the string ends with the specified string
String. prototype. EndsWith = function (A, B ){
Var C = this. length;
Var D = A. length;
If (D> C) return false;
If (B ){
Var E = new RegExp (A + '$', 'I ');
Return E. test (this );
} Else return (D = 0 | this. substr (C-D, D) = );
};
// Determine whether the string starts with the specified string
String. prototype. StartsWith = function (str)
{
Return this. substr (0, str. length) = str;
};
// Where does the string start?
String. prototype. Remove = function (A, B ){
Var s = '';
If (A> 0) s = this. substring (0, );
If (A + B Return s;
};

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.