Skip to content

Instantly share code, notes, and snippets.

View fengdi's full-sized avatar

tangoboy fengdi

View GitHub Profile
@slevithan
slevithan / xregexp-lookbehind2.js
Created April 14, 2012 21:06
Simulating lookbehind in JavaScript (take 2)
// Simulating infinite-length leading lookbehind in JavaScript. Uses XRegExp.
// Captures within lookbehind are not included in match results. Lazy
// repetition in lookbehind may lead to unexpected results.
(function (XRegExp) {
function prepareLb(lb) {
// Allow mode modifier before lookbehind
var parts = /^((?:\(\?[\w$]+\))?)\(\?<([=!])([\s\S]*)\)$/.exec(lb);
return {
@fengdi
fengdi / gist:5326735
Last active December 15, 2015 21:38
根据路径获取json对象的值 忽略异常返回值友好
function getValueByPath(obj, path){
(path+"").replace(/[^.:]+/g,function(n){
obj = (obj!=void 0 && (typeof obj=="object" || typeof obj=="function") && n in obj) ? obj[n] : void 0;
});
return obj;
}
// getValueByPath({a:[{c:"r"}]}, "a.0.c"); // r
@fengdi
fengdi / gist:5330459
Last active December 15, 2015 22:09
根据路径对一个json对象设置值 忽略异常
function setValueByPath(obj, path, value){
var temp, k, re = obj;
if(!obj || (typeof obj!="object" && typeof obj!="function"))
return obj;
(path+"").replace(/([^.:]+)([.:])?/g, function(m, n, sign){
temp = obj;
k = n;
obj = obj[n] = (obj!=void 0 && (typeof obj=="object" || typeof obj=="function") && n in obj) ? obj[n] : sign ==":" ? [] : {};
});
if(k && temp){