The purpose of this series of articles is to document some practical javascript tips, both as a knowledge accumulator and as a record of writing code in your spare time. At the same time, it is convenient to read ~ in the future

1. Implement base64 decoding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

function base64_decode(data){
var b64 = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”;
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,ac = 0,dec = “”,tmp_arr = [];
if (! data) { return data; }
data += ”;
do {
h1 = b64.indexOf(data.charAt(i++));
h2 = b64.indexOf(data.charAt(i++));
h3 = b64.indexOf(data.charAt(i++));
h4 = b64.indexOf(data.charAt(i++));
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;
if (h3 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1);
} else if (h4 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1, o2);
} else {
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
}
} while (i < data.length);
dec = tmp_arr.join(”);
dec = utf8_decode(dec);
return dec;
}

2. Implement utF8 decoding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

function utf8_decode(str_data){
var tmp_arr = [],i = 0,ac = 0,c1 = 0,c2 = 0,c3 = 0; str_data += ”;
while (i < str_data.length) {
c1 = str_data.charCodeAt(i);
if (c1 < 128) {
tmp_arr[ac++] = String.fromCharCode(c1);
i++;
} else if (c1 > 191 && c1 < 224) {
c2 = str_data.charCodeAt(i + 1);
tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = str_data.charCodeAt(i + 1);
c3 = str_data.charCodeAt(i + 2);
tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return tmp_arr.join(”);
}

3. Convert half Angle to full Angle function

1
2
3
4
5
6
7
8
9
10
11
12
13
14

function ToDBC(str){
var result = ”;
for(var i=0; i < str.length; i++){
code = str.charCodeAt(i);
if(code >= 33 && code <= 126){
result += String.fromCharCode(str.charCodeAt(i) + 65248);
}else if (code == 32){
result += String.fromCharCode(str.charCodeAt(i) + 12288 – 32);
}else{
result += str.charAt(i);
}
}
return result;
}

4. Convert full Angle to half Angle function

1
2
3
4
5
6
7
8
9
10
11
12
13
14

function ToCDB(str){
var result = ”;
for(var i=0; i < str.length; i++){
code = str.charCodeAt(i);
if(code >= 65281 && code <= 65374){
result += String.fromCharCode(str.charCodeAt(i) – 65248);
}else if (code == 12288){
result += String.fromCharCode(str.charCodeAt(i) – 12288 + 32);
}else{
result += str.charAt(i);
}
}
return result;
}

5. Use regular expressions to clear scripts in HTML code

1
2
3

function clear_script(html){
return html.replace(/

[\s\S]*? < \ / script > | \ s + on [a zA – Z] {3 dec} \ s? =\s?” [\s\S]*?” | \ s + on [a zA – Z] {3 dec} \ s? =\s? ‘[\s\S]*? ‘| \ s + on [a zA – Z] {3 dec} \ s? =[^ >]+/ig,””);
*?>
}

6. Get the current element style

1
2
3
4
5
6
7
8
9
10
11
12
13

function getStyle(oElm, strCssRule){
var strValue = “”;
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm, “”).getPropertyValue(strCssRule);
}
else if(oElm.currentStyle){
strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
return p1.toUpperCase();
});
strValue = oElm.currentStyle[strCssRule];
}
return strValue;
}

7. Format the CSS style code

1
2
3
4
5
6
7
8
9

Function formatCss(s){// Format code
s = s.replace(/\s*([\{\}\:\;\,])\s*/g, “$1”);
s = s.replace(/; \s*; /g, “;” ); // Clear consecutive semicolons
s = s.replace(/\,[\s\.\#\d]*{/g, “{“);
s = s.replace(/([^\s])\{([^\s])/g, “$1 {\n\t$2”);
s = s.replace(/([^\s])\}([^\n]*)/g, “$1\n}\n$2”);
s = s.replace(/([^\s]); ([^\s\}])/g, “$1; \n\t$2”);
return s;
}

8. Compress the CSS style code

1
2
3
4
5
6
7
8

Function compressCss (s) {// Compress code
s = s.replace(/\/\*(.|\n)*? \*\//g, “”); // Delete comments
s = s.replace(/\s*([\{\}\:\;\,])\s*/g, “$1”);
s = s.replace(/\,[\s\.\#\d]*\{/g, “{“); // error tolerance
s = s.replace(/; \s*; /g, “;” ); // Clear consecutive semicolons
s = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); // Remove the first and last whitespace
return (s == null) ? “” : s[1];
}

9. Common regular expressions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

/ / positive integer
/ ^ [0-9] * [1-9] [0-9] * $/;
/ / negative integer
/ ^ – [0-9] * [1-9] [0-9] * $/;
// A positive floating point number
/ ^ (([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) | ([0-9] * [1-9] [0-9] *)) $/;
// a negative floating point number
/ ^ (- (([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ [0-9] +) | ([0-9] * [1-9] [0-9] *))) $/;
/ / floating point number
/ ^ (-? \d+)(\.\d+)? $/;
/ / email address
/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
/ / url
/^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\? \S*)? $/;
Or: ^ HTTP: / / / / / A – Za – z0-9 + \. [A – Za – z0-9] + [\ / = \? % \ – & _ ~ ` @ \ [\] ‘: +!] * ([^ < > \ \ “”]) * $
// year/month/day (year-month-day, year. ) on.
/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/;
// Matches Chinese characters
/[\u4e00-\u9fa5]/;
// Whether the matching account is valid (starting with a letter, 5-10 bytes allowed, alphanumeric underscore allowed)
/ ^ [a zA – Z] [a – zA – Z0-9 _] {4, 9} $/;
// A regular expression that matches blank rows
/\n\s*\r/;
// Match Chinese zip code
/[1-9]\d{5}(? ! \d)/;
// Match the id card
/\d{15}|\d{18}/;
// Match a domestic phone number
/(\d{3}-|\d{4}-)? (\d{8}|\d{7})? /;
// Matches the IP address
/((2[0-4]\d|25[0-5]|[01]? \d\d?) \.) {3}(2[0-4]\d|25[0-5]|[01]? \d\d?) /;
// A regular expression that matches the first and last whitespace characters
/^\s*|\s*$/;
// A regular expression that matches HTML tags
< (\S*?) [^ >] * >. *? | <. *? / >;
/ / SQL statements
^(select|drop|delete|create|update|insert).*$
// Extract the network link from the information
(h|H)(r|R)(e|E)(f|F) *= *(‘|”)? (\w|\\|\/|\.) + (‘ | | | * “>)?
// Extract the email address from the message
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
// Extract the image link from the message
(s|S)(r|R)(c|C) *= *(‘|”)? (\w|\\|\/|\.) + (‘ | | | * “>)?
// Extract the IP address from the information
(\d+)\.(\d+)\.(\d+)\.(\d+)
// Get the Chinese mobile phone number in the message
(86)*0*13\d{9}
// Extract the Chinese zip code from the information
[1-9]{1}(\d+){5}
// Extract floating point numbers (i.e. decimals) from information
(-? \d*)\.? \d+
// Extract any numbers from the information
(-? \d*)(\.\d+)?
// Phone area code
^ 0 \ d {2, 3} $
// Tencent QQ id
^ (1-9] * [1-9] [0-9] * $
// Account number (starting with a letter, 5-16 bytes allowed, alphanumeric underscore allowed)
^ [a zA – Z] [a – zA – Z0-9 _] {4, 15} $
// Chinese, English, digits and underscores
^[\u4e00-\u9fa5_a-zA-Z0-9]+$

10. Format numbers and amounts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

function number_format(number, decimals, dec_point, thousands_sep) {
/ *
* Parameter description:
* number: indicates the number to be formatted
* Decimals: Preserve several decimals
* dec_point: decimal point symbol
* thousands_sep: thousandth symbol
* * /
number = (number + ”).replace(/[^0-9+-Ee.]/g, ”);
var n = ! isFinite(+number) ? 0 : +number,
prec = ! isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === ‘undefined’) ? ‘,’ : thousands_sep,
dec = (typeof dec_point === ‘undefined’) ? ‘.’ : dec_point,
s = ”,
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return ” + Math.ceil(n * k) / k;
};
s = (prec ? toFixedFix(n, prec) : ” + Math.round(n)).split(‘.’);
var re = /(-? \d+)(\d{3})/;
while (re.test(s[0])) {
s[0] = s[0].replace(re, “$1” + sep + “$2”);
}
if ((s[1] || ”).length < prec) {
s[1] = s[1] || ”;
s[1] += new Array(prec – s[1].length + 1).join(‘0’);
}
return s.join(dec);
}
Var num = of number_format (1234567.089, 2, “”,”, “); / / 1234567 15