The javascript date Formatting Function of meizz is very useful.
Original post see: http://blog.csdn.net/meizz/article/details/405708
Date.prototype.format = function(format) //author: meizz{ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o)if(new RegExp("("+ k +")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format;}alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
The code is well written, mainly using regular expressions and replace. I have simply made some enhancements and revisions,
The replace function is used to replace Regexp. $1.
Added the 24-hour representation of H.
The original H is in 12-hour format.
With the addition of the e-generation table week, you can extend it again.
/* The letter date or time element indicates the example G era identifier text ad y year 1996; month July in 96 m; Jul; 07 w in the week number 27 W in the week number 2 D in the year number 189 d in the month Number 10 F in the week number 2 E in the day text Tuesday; tue a AM/PM mark text pm h hour in a day (0-23) number 0 K hour in a day (1-24) number 24 k AM/PM hours (0-11) number 0 h am/PM hours (1-12) number 12 m hours minutes number 30 s Minutes number 55 s Ms number 978 Z Time Zone General Time Zone Pacific Standard Time; PST; GMT-08: 00 Z Time Zone RFC 822 Time Zone-0800 */string. prototype. repeat = function (count, seperator) {var seperator = seperator | ''; var A = new array (count); For (VAR I = 0; I <count; I ++) {A [I] = This;} return. join (seperator);} date. prototype. format = function (style) {var o = {"y {4} | y {2}": This. getfullyear (), // year "m {1, 2}": This. getmonth () + 1, // month "d {1, 2}": This. getdate (), // Day "H {1, 2}": This. gethours (), // Hour "H {1, 2}": This. gethours () % 12, // Hour "m {1, 2}": This. getminutes (), // minute "s {1, 2}": This. getseconds (), // second "E": This. getday (), // day in week "Q": math. floor (this. getmonth () + 3)/3), // quarter "s {3} | S {1}": This. getmilliseconds () // millisecond} For (var k in O) {style = style. replace (New Regexp ("(" + K + ")"), function (m) {return ("0 ". repeat (M. length) + O [k]). substr ("" + O [k]). length) ;}} return style ;}; document. write (new date (). format ('yyyy-mm-dd QQ we h: mm: SS ss '));
The Code seems a little less.
The replace function is very powerful. Learn from the following simple routine:
String.prototype.format = function(){ var args = arguments; return this.replace(/\{(\d+)\}/g, function(m,i){ return args[i]; });}var a = "I Love {0}, and You Love {1},Where are {0}! {4}";alert(a.format("You","Me"));
Stringobj.Replace Parameters
The first parameter is regular
The second parameter can be text, which is very simple or function, which is powerful.
The parameters of this function are dynamic. How many parameters are there? m + 3? What is m? The number of left parentheses in the regular expression. If there is a left parenthesis, then there are 1 + 3 = 4 parameters
So there are at least three parameters.
Parameter 1: matched substring
Parameter 2: All results captured in the search (usually the same as the first one, if () is on the outermost side)
Parameter 3: search for all results captured
......: Find all the results captured in
Parameter m + 2: InStringobjOffset of matching in
Parameter m + 3:StringobjItself
This is easy to understand.
Update:, replace the obfuscation string:
VaR o = "Sina"; // original string var Re =/s (. +) I (. +) n (. +) (. +)/g; // original string regular var S = "svserinfonoaction"; // obfuscated string // alert (Re. test (s); var r = S. replace (Re, function () {var T = arguments [0]; for (VAR I = 0; I <O. length; I ++) {T = T. replace (arguments [I + 1], "") ;}return t ;}); document. write (R );
Here are more detailed references:
Http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html