0

I was wondering if it is possible to extract an Array from a String using pure native javascript:

I tried using JSON.parse() and indexOf() but the return string has dynamic values in it making it hard to identify which first section is a whole of an array string format.

I was able to get it work using

let value = result;
value=value.substr(value.indexOf("["),value.indexOf("generic653")+1);
let splitter = JSON.parse(splitt);

console.log(splitter);

But right after a while when the values starts to change I get a parse error saying it's not an array.

Example string looks like this:

    let val = '>\n} som7833e)) 6585[["val.s","g.m",[1125,null,null,
["hsg.s",null,null]]\n,true,true,false,"generic653"]], 58["https",null,false]';


    /* sometimes this: */

    let val2 = ')[} eval668525)) 8895222["gt.m","g.m",[1125,null,null,
["hsg.s",null,null]]\n,true,true,false,"generic611",["https",null,false]]\n\n 7889[jyu.b,null,false,true]';

I want.

[["val.s","g.m",[1125,null,null, ["hsg.s",null,null]]\n,true,true,false,"generic653"]]

and

["gt.m","g.m",[1125,null,null,["hsg.s",null,null]]\n,true,true,false,"generic611",["https",null,false]]

only here so i could JSON.parse() it and use it as an array. I even tried using try catch so that if it returns an error it would change the indexOf("generic653") to indexOf("]]") but as It is said the values are dynamic and there is no way i could trap all 15 thousand++ of them.

4
  • You might be able to use regex patterns to find the array string. But you have to get correct regex. r =/([.*])+/; r.exec(string) Commented May 25, 2020 at 17:36
  • 1
    @vishnusandhireddy i am familiar with regex but i don't think regex could help me with this - maybe yes or maybe not. There's actually a lot arrays set in a single string and i only want to extract the first one. Commented May 25, 2020 at 18:00
  • if the string is in a proper json format, you can parse it, but if it's complete gibberish, and has no logical order to it, then you aren't going to be able to write any single function to parse it. You have to know what steps are being done to pack that string in order to unpack it. Commented May 28, 2020 at 0:41
  • based solely on the two examples you provided, if you remove everything before the first '[' and everything after the last ']', you might be left with a parsable json. Commented May 28, 2020 at 0:45

1 Answer 1

2

I think it is more simple to use a classic element.indexOf...

function extractInfo( txt, key )
  {
  let pKey = txt.indexOf(key)
  if  (pKey<0) throw `key ${key} not found`

  let pEnd = txt.indexOf(']]',pKey+key.length)
  if  (pEnd<0) throw 'double end brackets not found'

  let clos = 2, p = pEnd;
  for(;p--;)
    {
    switch(txt.charAt(p))
      {
      case '[': clos--; break;  
      case ']': clos++; break;  
      }
    if (clos===0) break;  
    }
  if (clos>0) throw 'first open bracket not found'
  return txt.substring(p,pEnd+2)
  }


let val1 = '>\n} som7833e)) 6585[["val.s","g.m",[1125,null,null, ["hsg.s",null,null]]\n,true,true,false,"generic653"]], 58["https",null,false]';
let val2 = ')[} eval668525)) 8895222["gt.m","g.m",[1125,null,null, ["hsg.s",null,null]]\n,true,true,false,"generic611",["https",null,false]]\n\n 7889[jyu.b,null,false,true]';

testExtract( val1, 'generic653')
testExtract( val2, 'generic611')
testExtract( val2, 'xyz')

function testExtract (val, key )
  {
  try
    { 
    res = extractInfo (val, key ) 
    console.log(key, ' --> ', res ) 
    }
  catch (err)
    {
    console.log('ERROR !', key, ' --> ', err ) 
    }    
  }
.as-console-wrapper { max-height: 100% !important; top:0 }

Is there anyway this could be done without the key? like testExtract (val); only.

may be that, but where is the logic?

function extractInfo( txt )
  {
  let pEnd = txt.lastIndexOf(']]' )
    , p    = (pEnd<0) ? 0 : pEnd
    , clos = 2
    ; 
  for(;p--;)
    {
    switch(txt.charAt(p))
      {
      case '[': clos--; break;  
      case ']': clos++; break;  
      }
    if (clos===0) break;  
    }
  return (clos>0) ? null : txt.substring(p,pEnd+2)
  }


let val1 = '>\n} som7833e)) 6585[["val.s","g.m",[1125,null,null, ["hsg.s",null,null]]\n,true,true,false,"generic653"]], 58["https",null,false]';
let val2 = ')[} eval668525)) 8895222["gt.m","g.m",[1125,null,null, ["hsg.s",null,null]]\n,true,true,false,"generic611",["https",null,false]]\n\n 7889[jyu.b,null,false,true]';

console.log('extracted 1 --> ', extractInfo(val1)  ) 
console.log('-----------')
console.log('extracted 2 --> ', extractInfo(val2)  )
.as-console-wrapper { max-height: 100% !important; top:0 }

Sign up to request clarification or add additional context in comments.

3 Comments

Ill check this out and i shall vote if it works for me. Its seems you use key1 and key2 that changes multiple times. It might return an error if it doesn't match. I will try to run this today and see what happens tomorrow. Thanks Jojo!
Just as i thought @Jojo it returned an error.. thanks for trying anyway
Thanks jojo, sorry for my delayed response... Its says something about the key is not found. Is there anyway this could be done without the key? like testExtract (val); only.

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.