简体   繁体   中英

Javascript regular expression match json in html

I'm writing a chrome extension and there is a function to parse html data from another website. And I need to parse two Json string placed inside a javascript tag as below which i want to use regular expression to capture them separately. I have success to capture the second part with the following regular expression:

/iDetailData\s+=\s+({[^]+});/g

but the first part seem harder as it can only match to the end of csrf token while it expected to match until the }; . I have used the following regex to capture the first json part:

/iDetailConfig\s+=\s+([^}]+)/g

Could you please help me if you have any experienced for this case?

<script type="text/javascript">
  var iDetailConfig = {
    'pageType': '',
    'pageid': 'laputa20150511141457',
    'offerid': '528011005304',
    'catid': '122238002',
    'dcatid': '53486008',
    'parentdcatid': '54546001',
    'isRangePriceSku': 'true',
    'isSKUOffer': 'true',
    'memberid': 'b2b-275072844861040',
    'loginId': '淘淘羊供应链',
    'islogin': 'false',
    'buyerUserID': '',
    'isTP': 'true',
    'isSlsjSeller': 'false',
    'loginurl': '',
    'topDomainTpl': '',
    'unit': '个',
    'priceUnit': '元',
    'isPreview': 'false',
    'isVirtualCat': 'false',
    'isLadderGroup': '',
    'refPrice': '339.00',
    'beginAmount': '1',
    'mergeVariable': 'asyncResourcesAliimg',
    'currentTime': '1488967781423',
    'isAccessSpecialSuppy': 'true',
    'domainType': '',
    'sourceType': 'freebuynowoffer',
    'hasConsignPrice': 'false',
    'mkcActivityId': '',
    'mkcActivityStartTime': '',
    'qrcode': 'https://gqrcode.alicdn.com/img?type=ali&amp;w=150&amp;h=150&amp;el=m&amp;text=http%3A%2F%2Fma.m.1688.com%2Frush.html%3Fsecret%3D8mNOFtQd',
    'minqrcode': 'https://gqrcode.alicdn.com/img?type=ali&amp;w=46&amp;h=46&amp;el=m&amp;text=http%3A%2F%2Fma.m.1688.com%2Frush.html%3Fsecret%3D8mNOFtQd',
    'officialActivityId': '',
    'userId': '2750728448',
    'end': 0,
    '_csrf_token': '9274f530e426ddb9af2d0f254d464fce'
  };

  var iDetailData = {
    "sku": {
      "price": "",
      "retailPrice": "",
      "canBookCount": "274",
      "saleCount": "45",
      "priceRange": [
        [1, 339.00],
        [2, 269.00]
      ],
      "priceRangeOriginal": [
        [1, 339.00],
        [2, 269.00]
      ],
      "skuProps": [{
        "value": [{
          "name": "黄白色(大象长颈鹿款))"
        }, {
          "name": "米白色(小羊小鸭款)"
        }, {
          "name": "蓝色(小牛小马款)"
        }, {
          "name": "粉色"
        }],
        "prop": "颜色"
      }, {
        "value": [{
          "name": "50*30*7/9cm"
        }],
        "prop": "尺寸规格"
      }],
      "skuMap": {
        "米白色(小羊小鸭款)&gt;50*30*7/9cm": {
          "canBookCount": 174,
          "skuId": 3201563335895,
          "specId": "7f68748bd9b704fa9708242ec570aa33",
          "saleCount": 3
        },
        "蓝色(小牛小马款)&gt;50*30*7/9cm": {
          "canBookCount": 44,
          "skuId": 3201563335894,
          "specId": "57e90efbfac8e7b69e73108a07d6198f",
          "saleCount": 10
        },
        "黄白色(大象长颈鹿款))&gt;50*30*7/9cm": {
          "canBookCount": 7,
          "skuId": 3201563335893,
          "specId": "3c4e75dbf21e70d5cea3dcb033bf0a9c",
          "saleCount": 7
        },
        "粉色&gt;50*30*7/9cm": {
          "canBookCount": 49,
          "skuId": 3201563335896,
          "specId": "5139112038a0bdef1f3d48e035448d48",
          "saleCount": 6
        }
      },
      "end": 0
    }
  };
  iDetailData.allTagIds = [];
  iDetailData.isSourcePromotion = false
  iDetailData.hasPurcharseMark = false
</script>

If you want your capture to include the }; , try this:

/iDetailConfig\s+=\s+([^}]+});/g

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