0

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>
4
  • Sorry, where are these JSON strings? Commented Mar 9, 2017 at 4:22
  • @Phil it is defined as variable as you can see: var iDetailConfig and var iDetailData Commented Mar 9, 2017 at 4:28
  • I don't understand your question then. What exactly are you trying to do? You already have the objects iDetailConfig and iDetailData. Commented Mar 9, 2017 at 4:36
  • Oops, It's my bad for not explained well. I'm writing a chrome extension and there is a function to parse html data from another website. And since this is html code of the website i want to parse, it not mine! Commented Mar 9, 2017 at 4:44

2 Answers 2

1

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

/iDetailConfig\s+=\s+([^}]+});/g
Sign up to request clarification or add additional context in comments.

1 Comment

god, you saved hours of trying for me. didn't notice that i have miss that comma, thank you!
0
/var iDetailConfig[\s\S]*?\};/g

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.