简体   繁体   中英

remove quotes around ONLY numeric values in string

var arr= '[{"a":"b","c":"521"}]';

let ds = arr.replace(/"(\d)"/g,"$1");

alert(ds);

Sample; I have tried the above along with various others.. cannot seem to get this to take effect on numeric values only..

Desired result would be:

[{"a":"b","c":521}]

ie

    let dss = JSON.stringify(arr);
    let ds = dss.replace(/"(\d)"/g,"$1");

I'd parse the JSON and use a reviver function to replace digit strings with just the digits:

 var arr= '[{"a":"b","c":"521"}]'; const parsed = JSON.parse( arr, (key, value) => typeof value === 'string' && /^\\d+$/.test(value) ? Number(value) : value ); console.log(JSON.stringify(parsed));

let ds = arr.replace(/"(\d+)"/g,"$1");

\\d之前添加+就像: (\\d+)

Your regular expression is looking for one digit add a + for one or more

 var arr= '[{"a":"b","c":"521"}]'; let ds = arr.replace(/"(\\d+)"/g,"$1"); console.log(ds);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM