String.prototype implementation of some of the JavaScript functions introduced _javascript skills

Source: Internet
Author: User
Copy Code code as follows:

String.prototype use

Batch substitution, for example: 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<a.length;i++) {
C=c.replace (A[i],b[i]);
};
return C;
};

Remove whitespace characters at both ends of a character
String.prototype.trim=function () {
Return This.replace (/(^[/t/n/r]*) | ( [/t/n/r]*$)/g, ');
};

Remove the white space character to the left of the character
String.prototype.ltrim=function () {
Return This.replace (/^[/t/n/r]/g, "");
};

Remove the white space character to the right of the character
String.prototype.rtrim=function () {
Return This.replace (/[/t/n/r]*$/g, "");
};

Returns the length of the character, one in Chinese 2
String.prototype.chineselength=function ()
{
Return This.replace (/[^/x00-/xff]/g, "* *"). Length;
};

Determines whether a 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) ==a);
};
Determines whether a string starts with the specified string
String.prototype.StartsWith = function (str)
{
Return this.substr (0, str.length) = = str;
};
How long does the string start from?
String.prototype.remove=function (a,b) {
var s= ';
if (a>0) s=this.substring (0,a);
if (a+b<this.length) s+=this.substring (a+b,this.length);
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.