JavaScript Custom Common methods 1th/2 page _javascript tips

Source: Internet
Author: User
Tags trim
For example, the character of the page processing, JS regular expression validation, and so on. Below I will be my own shallow development experience comprehensive network of complex resources slightly consolidated, save oneself later to use when again search. This series I will be usually used to sum up the functions, all as a trigger.
The code is cheap. See codes:
A common string-handling function

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

Remove white space characters at both ends of the string
String.prototype.Trim = function () {
return this. Replace (/(^ \s +) | (\s + $)/g, "");
}

Remove the whitespace characters from the character Fu Zuotun
String.prototype.LeftTrim = function () {
return this. Replace (/(^ [\s] *)/G, "");
}

Remove whitespace characters from the Fu Yi end of a word
String.prototype.RightTrim = function () {
return this. replace (/([\s] * $)/G, "");
}

/* Ignore case compare strings for equality
Note: Do not ignore case comparison with the = = number
String.prototype.IgnoreCaseEquals = function (str) {
return this. toLowerCase () = = Str.tolowercase ();
}

/* Do not ignore case comparison string Equality * *
String.prototype.Equals = function (str) {
return (this = = str);
}

/* Compare String, return 1, 0 based on result
The return value is the same: 0 is not the same:-1
*/
String.prototype.CompareTo = function (str) {
if (this = = str) {
return 0;
} else
return-1;
}

String substitution
String.prototype.Replace = function (OldValue, newvalue) {
var reg = new RegExp (OldValue, "G");
return this. Replace (Reg, newvalue);
}

Check whether to end with a specific string
String.prototype.EndsWith = function (str) {
return this. substr (this. length-str.length) = = str;
}

Determines whether a string starts with the specified string
String.prototype.StartsWith = function (str) {
return this. substr (0, str.length) = = str;
}

Intercept n characters from the left
String.prototype.LeftSlice = function (n) {
return this. substr (0, N);
}

Intercept n characters from the right
String.prototype.RightSlice = function (n) {
return this. substring (this. length-n);
}

Count the number of occurrences of a specified character
String.prototype.Occurs = function (ch) {
var re = eval ("/[^" +ch+ "]/g");
Return This.replace (Re, ""). Length;
return this. Split (CH). Length-1;
}


/* Constructs a string of specific styles, with <span></span> included * *
String.prototype.Style = function (Style) {
Return "<span style=\" ". Concat (Style," > ", this," </span> ");
}

Constructs a function similar to StringBuilder (used when connecting multiple strings, convenient)
function StringBuilder (str) {
this. Temparr = new Array ();
}
StringBuilder.prototype.Append = function (value) {
this. Temparr.push (value);
return this;
}
StringBuilder.prototype.Clear = function () {
this. temparr.length = 0;
}
StringBuilder.prototype.toString = function () {
return this. Temparr.join (');
}

String common methods and extensions
function Test () {
var teststr = "This is a test string";
var testStr2 = "string";
Alert (Teststr.trim ());
Alert (Teststr.lefttrim ());
Alert (Teststr.righttrim ());
Alert (Teststr2.chineselength ());
Alert (Teststr2.compareto (TESTSTR));
Alert (Teststr2.startswith ("string"));
document.write (Teststr2.style ("color:red;width:100px"));
Alert (Teststr2.leftslice (2));
Alert (Teststr.rightslice (7));
Alert (teststr.occurs ("s"));
/* StringBuilder Test * *
var testStr3 = new StringBuilder ("");
Teststr3.append ("test3\r\n");
Teststr3.append ("test3test3\r\n");
Teststr3.append ("Test3");
Alert (teststr3.tostring ());
Teststr3.clear ();
Alert (teststr3.tostring ());
}

Second, commonly used regular expressions

/*-----------------------The following function still involves some string processing, but looks more reasonable as part of a regular expression-----------------------------
Check whether a string is made up of numbers
String.prototype.IsDigit = function () {
var str = this. Trim ();
Return (Str.replace (/\d/g, ""). Length = = 0);
}

Verify that the string is a floating-point type
String.prototype.IsFloat = function () {
var str = this. Trim ();
If blank, the checksum is not passed
if (str = "")
return false;
If it is an integer, verify the validity of the integer
if (Str.indexof (".") = = 1) {
Return str. IsDigit ();
}
else {
if (/^ (\-?) (\d+) (. {1}) (\d+) $/g.test (str))
return true;
Else
return false;
}
}

Check if a negative integer
function Isnegativeinteger (str) {
If blank, the checksum is not passed
if (str = "")
return false;
if (str. IsDigit ()) {
if (parseint (str, ten) < 0)
return true;
Else
return false;
}
Else
return false;
}

Check if the number of negative floating-point numbers
function Isnegativefloat (str) {
If blank, the checksum is not passed
if (str = "")
return false;
if (str. Isfloat ()) {
if (parsefloat (str, ten) < 0)
return true;
Else
return false;
}
Else
return false;
}

is a string composed of letters
function Ischaracter (str) {
Return (/^[a-za-z]+$/. Test (str));
}

is a string of letters and numbers
function Isnumbercharacter (str) {
Return (/^[a-za-z0-9]+$/. Test (str));
}

Whether it's an email
function Isemail (str) {
Return (/(\s) +[@]{1} (\s) +[.] {1} (\w) +/. Test (STR))
}

is the URL (commentary: The online version of the function is very limited, the following basic to meet the needs)
function Isurl (str) {
return (/([a-za-z]+:\/\/)? [ ^s]*/. Test (str));
}

Whether it is an IP address
function Isipaddress (str) {
return/(\d+) \. (\d+) \. (\d+) \. (\d+)/. Test (STR);
}

is a string of Chinese characters
function Ischinese (str) {
Return (/^[\u4e00-\u9fa5]+$/. Test (str));
}

is a double-byte character (including Chinese characters)
function Isunicode (str) {
Return (/^[\x00-\xff]+$/. Test (str));
}

is the phone number
function Istelephone (str) {
Compatible formats: Country code (2 to 3 bits)-area code (2 to 3 digits) (-)? Telephone Number (7 to 8 digits)-Extension number (3 digits)
Return (/^ ([0\+]\d{2,3}-)? 0\d{2,3}))? [-]? (\d{7,8}) (-(\d{3,}))? $/. Test (str));
}

Whether it's a cell phone number
function Ismobilephone (str) {
Return (/^ (\ (\d{3}\)) | ( \d{3}\-))? 1[3,5]\d{9}$/. Test (str));
}

QQ Number (Tencent QQ number starting from 10000)
function Isqqnumber (str) {
Return (/^[1-9][0-9]{4,}$/. Test (str));
}

Is the domestic zip code (6 digits for China ZIP code)
function Ismailcode (str) {
Return (/\d{6}/. Test (str));
}

Whether it is the domestic ID number
function Isidnumber (str) {
Return (/\d{15}|\d{18}/. Test (str));
}

On the regular expression, there are many in-depth articles on the Internet, I copy a few pieces of commonly used code here, in fact, learn the basic regular knowledge after the common verification is a piece of cake, no longer repeat.
Current 1/2 page 12 Next read the full text
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.