Need to get the Prospect ID from the email body. But the usual split() method doesn’t work all the time. So switched to match() method, which returns the first occurrence of this pattern in the string.
Example: This is the pattern , [Prospect:CUS234563]. Want to extract the “CUS234563” only. we first define a regular expression that matches the pattern “CUS” followed by one or more digits.
If a match is found, the match() method returns an array with the matched substring as its first element. We then extract this substring and assign it to the cusValue variable.
If no match is found, we output a message indicating that the “CUS” value was not found.
The code snippet is given below.
const str = "[Prospect: CUS234563, Oppor:8056]";
const regex = /CUS\d+/;
const match = str.match(regex);
if (match) {
const cusValue = match[0];
console.log(cusValue); // Output: "CUS234563"
} else {
console.log("CUS value not found");
}