如下jsText
是带有$.extend
函数字样的文本,期望通过ignoreExtend
方法去除$.extend
函数字样,如何实现ignoreExtend
?
let jsText = `
$.extend({}, x.x, {
xx: {},
xxx: $.extend({}, x.x, {
xx: {}
}),
xxxx: {
default: () => ({})
},
xxxx: $.extend({}, x.x, {
xx: {}
})
})`;
function ignoreExtend (jsText) {
// TODO
return jsText;
}
jsText = ignoreExtend(jsText);
console.log(jsText);
/*
{
xx: {},
xxx: {
xx: {}
},
xxxx: {
default: () => ({})
},
xxxx: {
xx: {}
}
}
*/
已做尝试,但是效果还差一点,括号没能都正确匹配上
function ignoreExtend (jsText) {
return jsText.replace(/\$\.extend\(\{\},\s*.+,\s*(\{[\s\S]+?\})\)/g, '$1');
}
/*
{
xx: {},
xxx: $.extend({}, x.x, {
xx: {}
},
xxxx: {
default: () => ({})
},
xxxx: {
xx: {}
}
})
*/
我也补充一种常规思路