3
\$\begingroup\$

I need to get the value of item inside of this string and parse it as JSON. I've got some working code but I feel like it could be optimized (a lot) and cleaned up (a lot). Any tips or pointers are appreciated. Thanks!

My current code:

let result  = script.match(/item:(.*|[\s\S]*)onSelected/gm)[0];
result = result.replace('onSelected', '').replace('item:', '').trim().slice(0, -1);
JSON.parse(result);

Here's the string of javascript I need to pull from:

jQuery(function($) {

    new Selector('itemSelect', {
      item: {"id":9343513159,"title":"The thing","description":"\u003cp\u003eThis is the description\u003c\/p\u003e\n\u003cp\u003eIt has details\u003c\/p\u003e\n","published_at":"2016-12-10T10:08:00-05:00","available":true,"individuals":[{"id":34972221767},{"id":34972221768},{"id":34972221769}],"images":["\/\/cdn.site.com\/s\/files\/1\/0094\/2252\/items\/image.jpg?v=1481382473"],"options":["Time"],"content":""},
      onSelected: selectCallback,
      enableState: true
    });


   // Some stuff here

    $('.main:eq(0)').prepend('<label class="forever" for="time">Forever</label>');

});

window.mainItem = window.mainItem || {};

mainItem.variables = {
  available : false
};

(function() {
  if (true) {
    mainItem.variables.available = true;
  }
})();
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Can you add complete code of Selector function? I'm assuming the code you've wrote is inside it. So, there is no need of regex to extract value from object. \$\endgroup\$
    – Tushar
    Commented Jan 23, 2017 at 3:16

1 Answer 1

2
\$\begingroup\$
  • Use .exec to match a group
  • Add comma removed by slice directly into the regexp
  • Use capturing groups: query for [1], not for [0]

let script = document.getElementById('source').text;
var result = /item:(.*|[\s\S]*),\s*onSelected/gm.exec(script)[1];
console.log(JSON.parse(result));
<script id="source" type="text/plain">
jQuery(function($) {
      
        new Selector('itemSelect', {
          item: {"id":9343513159,"title":"The thing","description":"\u003cp\u003eThis is the description\u003c\/p\u003e\n\u003cp\u003eIt has details\u003c\/p\u003e\n","published_at":"2016-12-10T10:08:00-05:00","available":true,"individuals":[{"id":34972221767},{"id":34972221768},{"id":34972221769}],"images":["\/\/cdn.site.com\/s\/files\/1\/0094\/2252\/items\/image.jpg?v=1481382473"],"options":["Time"],"content":""},
          onSelected: selectCallback,
          enableState: true
        });
      
    
       // Some stuff here
      
        $('.main:eq(0)').prepend('<label class="forever" for="time">Forever</label>');
      
    });
    
    window.mainItem = window.mainItem || {};
    
    mainItem.variables = {
      available : false
    };
    
    (function() {
      if (true) {
        mainItem.variables.available = true;
      }
    })()
</script>

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.