Reputation: 116
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.
Upvotes: 0
Views: 93
Reputation: 22345
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 }
Upvotes: 2