87,910
社区成员
发帖
与我相关
我的任务
分享
var str = `{
"x":[{
"a":1,
"b":[12344,12345678901234567],
"c":"[12345678901234567]\\\\",
"t":"aaa\\\\\\"bbb:12345678901234567",
"d":5678.12345678901234567,
"e":"{ \\\"abc\\\":12345678901234567, \\\"12345678901234567\\\":[12345678901234567] }",
"f":[-12345678901234567]
}]
}`;
alert(str);
str=str.replace(/((?:([\"\'])(?:\\.|[^\\])*?\2[^\"\']*?)*?)([\-\+]?[\d\.]{17,})/g,'$1"$3"');
alert(str);
var str = `{
"x":[{
"a":1,
"b":[12344,12345678901234567],
"c":"[12345678901234567]\\\\",
"t":"aaa\\\\\\"bbb:12345678901234567",
"d":5678.12345678901234567,
"e":"{ \\\"abc\\\":12345678901234567, \\\"12345678901234567\\\":[12345678901234567] }",
"f":[-12345678901234567]
}]
}`;
alert(str);
str=str.replace(/((?:([\"\'])(?:\\.|[^\\])*?\2[^\"\']*?)*?)([\-\+]?[\d\.]{17,})/g,'$1"$3"');
alert(str);
`{ a123456789123456789: 456123456*0x0000000000000000000001+50000001234567 }`.replace(/"[^"]+"|[^:,\{\[\]\}]+/gi,function(e){return /^[^"]{16,}/gi.test(e)?'"'+e+'"':e})
`{"a":11111111111111111,"b":[{"a":11111111111111111,"b":[12344,456,1234567890123456789],"c":"[12345678901234567]","d":12345678901234567,"e":"{12345678901234567:4555466465488456546}"}],1234567890123456789:1213}`.replace(/"[^"]+"|[^:,\{\[\]\}]+/gi,function(e){return /^[0-9]{16,}$/gi.test(e)?'"'+e+'"':e})
`{"a":11111111111111111,"b":[{"a":11111111111111111,"b":[12344,456,1234567890123456789],"c":"[12345678901234567]","d":12345678901234567,"e":"{12345678901234567:4555466465488456546}"}],1234567890123456789:1213}`.replace(/"[^"]+"|[^:,\{\[\]\}]+/gi,function(e){return /^[0-9]{16,}/gi.test(e)?'"'+e+'"':e})
var str = `{
"a":1,
"b":[
{
"a":1,
"b":[12344,456],
"c":"[12345678901234567]",
"d":5678.12345678901234567,
"e":"{ \\\"abc\\\":12345678901234567, \\\"12345678901234567\\\":[12345678901234567] }",
"f":[-12345678901234567]
}
]
}`;
str.replace(/("(?:\\u[\da-f]{4}|\\[\d\D]|[^\\"])*")|([^"]+)/ig,
function($, $1, $2) { if ($1) return $1; return $2.replace(/[\d\.-]{17,}/, '"$&"');
});
如果要考虑非标准JSON,那就不是写个正则替换就好的了,要做语法分析的
像 js 直接量 obj = { a123456789123456789: 456123456*0x0000000000000000000001+50000001234567 } 这种
var str = '{"a":"12345678901234567","c":{"b":"{\"a\":\"12345678901234567\"}"},"d":"{\"a\":\"12345678901234567\",\"c\":{\"b\":\"{\\\"a\\\":\\\"12345678901234567\\\"}\"}}"}
';
var str = `{
"a":1,
"b":[
{
"a":1,
"b":[12344,456],
"c":"[12345678901234567]",
"d":12345678901234567,
"e":"{ \"abc\":12345678901234567, \"12345678901234567\":[12345678901234567] }",
"f":[12345678901234567]
}
]
}`;
str.replace(/("(?:\\u[\da-f]{4}|\\[\d\D]|[^\\"])*")|([^"]+)/ig,
function($, $1, $2) { if ($1) return $1; return $2.replace(/([\W]\s*)(\d{17,})/, '$1"$2"');
});
var str = `{"a":1,"b":[{"a":1,"b":[12344,456],"c":"[12345678901234567]","d":12345678901234567,"e":"{12345678901234567:12345678901234567}"}]}`;
str=str.replace(/([\"\']).*?\1/g,n=>n.replace(/\d{16}/g,"$&\x01")).replace(/\d{17,}/g,'"$&"').replace(/\x01/g,"");
alert(str);
var str = `{"a":1,"b":[{"a":1,"b":[12344,456],"c":"[12345678901234567]","d":12345678901234567,"e":"{12345678901234567:45646}"}]}`;
str=str.replace(/(:)(\d{17,})/g,'$1"$2"');
alert(str);
var str = `{"a":1,"b":[{"a":1,"b":[12344,456],"c":"[12345678901234567]","d":12345678901234567,"e":"{12345678901234567:45646}"}]}`;
var reg=/:(\d{16,})/
var str2=str.replace(reg,':"$1"');
console.log(str2);
这样试试