正则表达式的日期和数字值没有引号

时间:2018-05-03 12:01:12

标签: json regex

我有一个长约40K行的JSON(类似于下面),我需要一个正则表达式exp,以便我能找到&用正常数值替换所有双引号数值。

这是输入的例子:

"date": "5/9/2026",
"cost": 33,300,000.45",
"insured": "33,295,198.12",

预期产出:

"date": 5/9/2026,
"cost": 33,300,000.45,
"insured": 33,295,198.12,

我在stackoverflow上搜索,跟随各自的灵魂,但没有达到我预期的输出。

我的尝试:"([0-9]*\,[0-9]*\,[0-9]*\,.[0-9])"

2 个答案:

答案 0 :(得分:0)

替换后,它不会是有效的json,如果它对您没有关系,那么请使用此代码:



var str = "\"date\": \"5/9/2026\",	\"cost\": \"33,300,000.45\", \"insured\": \"33,295,198.12\",";

var mapReplace = {
       ": \"":": ",
       "\",":",",
    };
    var res = str.replace(/: "|",/g,function(match){
        return mapReplace[match];
      });
      
      console.log(res);




答案 1 :(得分:0)

使用正向lookbehind和负向前瞻,以下正则表达式应该为您提供预期的输出:

(?<=:\s)"?([0-9]{1,3}(?:\,[0-9]{3})*(?:\.\d+)?)(?!\/)"?

Demo

相关问题