regex - Parse Values from String Using Javascript -


trying find efficient way extract values large string.

ext-x-daterange:id="preroll_ident_open",start-date="2016-12-14t120000.000z",duration=3,x-playheadstart="0.000",x-adid="aa-1qpn49m9h2112",x-transaction-vprn-id="1486060788",x-trackingdefault="1",x-trackingdefaulturi="http,//606ca.v.fwmrm.net/ad/l/1?s=g015&n=394953%3b394953&t=1485791181366184015&f=&r=394953&adid=15914070&reid=5469372&arid=0&auid=&cn=defaultimpression&et=i&_cc=15914070,5469372,,,1485791181,1&tpos=0&iw=&uxnw=394953&uxss=sg579054&uxct=4&metr=1031&init=1&vcid2=394953%3a466c5842-0cce-4a16-9f8b-a428e479b875&cr="s=0&iw=&uxnw=394953&uxss=sg579054&uxct=4&metr=1031&init=1&vcid2=394953%3a466c5842-0cce-4a16-9f8b-a428e479b875&cr=" 

i have above example. idea extract caps string before : object key, , in between quotes until next comma value. iterate entire string until object created.

nonparsed.substring(nonparsed.lastindexof("="")+1, nonparsed.lastindexof("",")); 

i had concept start, iterating through , making more efficient appreciated.

final output --

{   'ext-x-daterange:id': 'preroll_ident_open',   'start-date': '2016-12-14t120000.000z',   'duration': '3',   ... } 

it looks property messes predictable pattern duration, followed number. otherwise, can rely on naive pattern of alternating =" , ",.

you like

str = str.replace(/duration=(\d+)/, `duration="$1"`); return str.split('",').reduce((acc, entry) => {     let key = `'${entry.split('="')[0]}'`;     let value = `'${entry.split('="')[1]}'`;     acc[key] = value;     return acc; }, {}); 

then add bit of logic end sort out duration if needed to.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -