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;
 }
})();
t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Jan 22, 2017 at 16:39
\$\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\$ 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>

answered Jan 22, 2017 at 20:59
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.