Looking to remove any value after :->

I am looking to remove any value after ':->' in JavaScript For example if 'Packet:->3', I would like to store "Packet:->" in the variable so I can use it before the if statement seen below. Pretty much I am looking to remove any digits after '>'

I was trying the below, but did not have much luck.

NTEremoved = NTE.indexOf(':->');

What would be the best way of doing this?

if(NTE == 'Packet:->' || NTE == 'Serving:->' || NTE == 'Can/Carton/Bottle:->'){

} 

Solution 1:

String split.

const testStr1 = "Packet:->3";
const testStr2 = "BlahBlahBlah278:->Hello hello this is cool";
const result1 = testStr1.split(":->")[0] + ":->"; // split "splits" the string into an array based on the delimiter ":->"; the second part would be 3
const result2 = testStr2.split(":->")[0] + ":->";
console.log(result1);
console.log(result2);

Docs.

Solution 2:

I would do something like this, to take care also of edge cases (assuming you don't care about what is coming after the first :->:

let texts = ["Packet:->11", "Cat:->22", "Packet:->:->:->44"];

for (let text of texts) {
  console.log(text.replace(/(:->).*$/, "$1"))
}

Solution 3:

Since you have mentioned you have tries to use indexOf before but fail to do that, I will provide you a way using a combination of indexOf and String.slice

let string = "Serving:->3"
//Indexof return the first index of an element so you have to add 3 (the length of ":->"
let newstring = string.slice(0,string.indexOf(":->")+3)

console.log(newstring)