Find substring , extract it with its corresponding value from String source

Good Day Everyone

I have an Issue, hope you can help me .

I have a string tag in the Response, that string looks like this
"|Search=name|codProd=ABC_73335610|CNAD_ENTRY=1551|UFO=pa|UFD=sa|natOp=111|

What I need is to extract a substring for instance “UFO” from pipe to pipe , final result will be like UFO=pa , then save it into a variable
Also not all string will have UFO so Need to be able to consider that.

You can use split() to create an array

let string = "|Search=name|codProd=ABC_73335610|CNAD_ENTRY=1551|UFO=pa|UFD=sa|natOp=111";
var array = string.split('|');
console.log(array);

image

You can target the element directly by its array index.

console.log(array[4]);

Or you can use the JavaScript find function.

let search = array.find(element => element.includes("UFO"));
console.log(search); 

Both ways will return the “UFO=PA” string.

image

2 Likes

Good day mdjones , as always thank you very much ,for your help