Extract text from between characters in string

jonathangilbert
Kilo Sage

Hi,

 

I have been searching the community but not actually found the solution. I need to extract the text between the the 2 "|" as shown in the below example:-

 

TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS
 
So i would expect "ABNORMAL_ANONYMOUS_IP" to be returned and then updated into the 
"u_report_name" field.
The TICKET ID number can change in length and also the text between the 2 "pipes" will also vary.
 
I have found a script on the forum which dealt with multiple emails in a string field,which I have ammended and shown below, but it does not work 
 
var str = 'TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS';
var split1 = str.split(";");
var stringArray = [];
for(var i=0;i<split1.length-1;i++){
var initialString = split1[i];
var cat  = initialString.substring(initialString.indexOf("|") + 1, initialString.indexOf("|"));
stringArray.push(cat.toString());
}
current.u_report_name = cat;
 
1 ACCEPTED SOLUTION

Valmik Patil1
Kilo Sage

Hello @jonathangilbert ,

 

Try below script

var input = "TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS";
var parts = input.split("|");
var result = parts.length > 1 ? parts[1].trim() : "";
gs.info("Extracted value: " + result);

and below is the updated script of yours,

var str = 'TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS';
var split1 = str.split(";");
var cat = parts.length > 1 ? parts[1].trim() : "";
current.u_report_name = cat;

Thanks,

Valmik Patil

View solution in original post

5 REPLIES 5

Shivalika
Mega Sage

Hello @jonathangilbert 

 

Use below 👇 regex pattern 

 

const text = "TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS";

const match = text.match(/\|([^|]+)\|/);

if (match) {

    const extractedText = match[1].replace(/_/g, ' ');

    console.log(extractedText); // Output: "ABNORMAL ANONYMOUS IP"

}

 

I hope this answered your query. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Valmik Patil1
Kilo Sage

Hello @jonathangilbert ,

 

Try below script

var input = "TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS";
var parts = input.split("|");
var result = parts.length > 1 ? parts[1].trim() : "";
gs.info("Extracted value: " + result);

and below is the updated script of yours,

var str = 'TICKET ID 924209 | ABNORMAL_ANONYMOUS_IP | JOE BLOGGS';
var split1 = str.split(";");
var cat = parts.length > 1 ? parts[1].trim() : "";
current.u_report_name = cat;

Thanks,

Valmik Patil

Morning Valmik,

 

Thanks for that it works perfectly and thanks for the quick response

Shivalika
Mega Sage

Hello @jonathangilbert 

 

Did you try my solution ? Did it not working for you ? 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY