Ask Your Question
1

unable to extract substring from string in javascript evaluator

asked 2018-05-24 04:33:40 -0500

amank gravatar image

updated 2018-05-24 10:29:13 -0500

metadaddy gravatar image

I am trying to extract data from the string i use index and substring function in java script to find the value.

var str = "May 21 16:00:03 localhost TM:78 2018-05-21 16:00:03 [HIP:239839484][PDU_TYPE:bfjbhfwjb][URL_TYPE:3][UUID : c21d8dd6-cc12-40f2-963b-5e732f9e2337][SVC : yunuyye][PASS : null][TO : 978][SMS_COUNT : 1][DATA_CODING : 0][IS_SHORT:true][FROM : tonwork][TON : 5][NPI : 0][ESM_CLASS : 64][DLR : 1][SM_LEN : 159][CIP : null][VP:null][ACT:null][DEST:786][X-Mannel-smppbox-submit_sm.meta-dataSmsPlus2||-||214||-||WorkXI||-||46||-||etisalat||-||0.01180||-||0.01560||-||0.01200||-||ytsrt_route||-||0.01030||-||-1|1187[TIME:6]"; 

var n = str.indexOf("Mannel");

var indexOfTimeStr = str.indexOf("TIME");

var res = str.substring(n,indexOfTimeStr-3);

document.write(res);

My desired Output:- image description

But while using in JS evaluator in streamsets I got this problem

image description

This is what I have tried in JS script

var startIndex = records[i].value['text'].indexOf(records[i].value['text'],"Mannel");

var endIndex = records[i].value['text'].indexOf(records[i].value['text'],"TIME");

records[i].value['text']= records[i].value['text'].substring(records[i].value['text'],startIndex,endIndex-3);
edit retag flag offensive close merge delete

Comments

1

Your snippet at the end is not the same as before the screenshot. The indexOf function in Javascript takes one argument only. In your snippet at the end you are passing two, which is not correct.

jeff gravatar imagejeff ( 2018-05-24 10:25:36 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-05-28 09:45:19 -0500

metadaddy gravatar image

Correct your JavaScript as Jeff suggested in the comments and it works just fine:

var startIndex = records[i].value['text'].indexOf("Mannel");

var endIndex = records[i].value['text'].indexOf("TIME");

records[i].value['text'] = records[i].value['text'].substring(startIndex,endIndex-3);

image description

edit flag offensive delete link more

Comments

Thank you that help's me to solve this problem,!!

amank gravatar imageamank ( 2018-05-30 00:30:32 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-05-24 04:33:40 -0500

Seen: 92 times

Last updated: May 28