String.prototype.format=function(){
if(arguments.length==0) return this;
for(var s=this, i=0; i<arguments.length; i++)
s=s.replace(new RegExp("\\{"+i+"\\}","g"), arguments[i]);
return s;
};
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
function parseUrl(url) {
var matcher = url.match(/^(?:(\w+):\/\/)?([^#:\/?]+)(?::(\d+))?(?:(\/[^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/);
if (matcher == null) {
throw new Error("not match");
}
var result = {
data: names = ['url', 'protocol', 'host', 'port', 'path', 'arg', 'anchor'],
protocol: 'http',
port: 80
};
for (var i = 0; i < result.data.length; i++) {
if (matcher[i] || matcher[i] === 0) {
result[result.data[i]] = matcher[i];
}
}
if (result['arg']) {
result['argobj'] = parseArg(result['arg']);
}
return result;
}
function parseArg(arg) {
var result = {};
var temp = arg.split('&');
for (var i = 0; i < temp.length; i++) {
var kv = temp[i].split('=');
result[kv[0]] = kv[1];
}
return result;
};
var url = "https://www.test.com:8080/main/tag/index.html?itemId=123&order=2#main"
console.info(parseUrl(url));
function deepSplit(str, separators, formatObj) {
var arr = [];
var temp1 = str.split(separators[0]);
var subSeparator = separators.slice(1);
if (subSeparator.length == 0)
return formatObj ? {[temp1[0]]: temp1[1]} : temp1;
for (var j = 0; j < temp1.length; j++)
arr.push(deepSplit(temp1[j], subSeparator, formatObj))
return arr;
}
var content = `a 1
aa 11
ab 12
ac 13
b 2
bb 22
bc 23
bd 24`
console.info(deepSplit(content, ['\n\n', '\n', ' ']));
console.info(deepSplit(content, ['\n\n', '\n', ' '], true));