Parsing a text string the right way?

Andrew Bettcher
Kilo Sage

Hi,

We get emails in from a certain place. They all have the same subject and the actual error is in the body. It's not formatted in such a way that I can use name:value pairs to pick it out so I'm trying to parse the text to grab the string.

The problem is that the string can vary in length. Here is my code:

var ecaBody = email.body_text;

	var shortDesc = ecaBody.substr(ecaBody.indexOf('Details')+10,31);
	current.short_description = current.short_description + ' - ' + shortDesc;

That starts getting the text at the right place (i.e. just after "Details"). I want it stop before the comes the a '[' character rather than me specifying the length. To demonstrate, this is the result from an email that has the appropriately length error string:

find_real_file.png

But, this is the result from one with a shorted length:

find_real_file.png

I can rely on the next unrequired character being a left square bracket - '['.

Can I say, take substring from the body after "details" and before "["?

1 ACCEPTED SOLUTION

palanikumar
Mega Sage

To stop at a specific character use split function. Refer the code below

var shortDesc = ecaBody.substr(ecaBody.indexOf('Details')+10).split("[")[0];

Thank you,

Palani

Thank you,
Palani

View solution in original post

3 REPLIES 3

palanikumar
Mega Sage

To stop at a specific character use split function. Refer the code below

var shortDesc = ecaBody.substr(ecaBody.indexOf('Details')+10).split("[")[0];

Thank you,

Palani

Thank you,
Palani

Fantastic mate. Nice one:

find_real_file.png

@palanikumar 

This doesn't seem to work for me every time.

My alert email is:
#
# Celerra celerraSerial:APM00152427017, Control Station: pdusdc1mgtvnx101
#
************* Apr 24, 2022 12:00:21 PM *************
Alert W1:
Storage pool 'US_VNX5800_Storage_Pool' is using 98.0% of its maximum capacity.

For recommended action, use the 'nas_message -i' CLI command with associated Message ID 87518806228

 

I pick it out with the code you showed me:

if (email.body.alert_w1 != undefined){
	email.body_text = email.body_text.replace(/\s+/g, '');
	email.body_text = email.body_text.substr(email.body_text.indexOf('Alert W1:')+9).split(".")[0];
	//current.short_description = email.body.alert_w1;
	gs.info('XX W1' + email.body_text);

But the result is this:

acelerraSerial:APM00152427017,ControlStation:pdusdc1mgtvnx101#*************Apr24,202212:00:21PM*************AlertW1:Storagepool'US_VNX5800_Storage_Pool'isusing98

 

It stops in the right place but starts 9 characters in rather than where I tell it with indexOf. What am I doing wrong?