如何使用正则表达式从Javascript中的字符串提取Json数组?

我有字符串输入为

        subtitle: {
            text: '(Linear Scale)'
        },

        xAxis: {
            categories: ["Jan 22","Jan 23","Jan 24","Jan 25","Jan 26","Jan 27","Jan 28","Jan 29","Jan 30","Jan 31","Feb 01","Feb 02","Feb 03","Feb 04","Feb 05","Feb 06","Feb 07","Feb 08","Feb 09","Feb 10","Feb 11","Feb 12","Feb 13","Feb 14","Feb 15","Feb 16","Feb 17","Feb 18","Feb 19","Feb 20","Feb 21","Feb 22","Feb 23","Feb 24","Feb 25","Feb 26","Feb 27","Feb 28","Feb 29","Mar 01","Mar 02","Mar 03","Mar 04","Mar 05","Mar 06","Mar 07","Mar 08","Mar 09","Mar 10","Mar 11","Mar 12","Mar 13","Mar 14","Mar 15","Mar 16","Mar 17","Mar 18","Mar 19","Mar 20","Mar 21","Mar 22","Mar 23","Mar 24","Mar 25","Mar 26","Mar 27","Mar 28","Mar 29","Mar 30","Mar 31","Apr 01","Apr 02","Apr 03","Apr 04","Apr 05","Apr 06","Apr 07"]       
         },

        yAxis: {
            title: {
                text: 'Total Coronavirus Cases'
            },

 series: [{
            data: [580,845,1317,2015,2800,4581,6058,7813,9823,11950,14553,17391,20630,24545,28266,31439,34876,37552,40553,43099,45134,59287,64438,67100,69197,71329,73332,75184,75700,76677,77673,78651,79205,80087,80828,81820]

         }],

我想要的是使用正则表达式提取“类别”和“数据”数组。

I've tried to access individual object via \{.*\:\{.*\:.*\}\} but not working

评论
  • eearum
    eearum 回复

    您可以为此使用split。

    // Assume above string is allString and special characters are escaped.  
    var categories = allString.split("categories: [")[1].split("]")[0];
    var data = allString.split("data: [")[1].split("]")[0];
    

    只是一个粗略的想法。让我知道事情的后续!