Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created January 5, 2025 05:31
Show Gist options
  • Save salmanx/54713c06d5a4880a99ad5795b5958fd1 to your computer and use it in GitHub Desktop.
Save salmanx/54713c06d5a4880a99ad5795b5958fd1 to your computer and use it in GitHub Desktop.
Format a number in a paragraph
function reformatPolicyNumbers(paragraph) {
const policyNumbers = paragraph.split(" ");
policyNumbers.map((word, index) => {
const policyNumber = word.split("-");
if (policyNumber.length === 3 && !isNaN(Number(policyNumber.join("")))) {
policyNumbers[index] = policyNumber.join("/");
}
});
return policyNumbers.join(" ");
}
const paragraph1 = "Your long-term policy number is 12-39-8552";
const paragraph2 =
"Your long-term policy number is 12-39-8552 and short term policy number is 65-98-789";
const paragraph3 = "Your 1st-policy-number policy number is 12-39-8552";
console.log(reformatPolicyNumbers(paragraph1)); // Your long-term policy number is 12/39/8552
console.log(reformatPolicyNumbers(paragraph2)); // Your long-term policy number is 12/39/8552 and short term policy number is 65/98/789
console.log(reformatPolicyNumbers(paragraph3)); // Your 1st-policy-number policy number is 12/39/8552
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment