Home > Enterprise >  Extract text until second to last occurrence from string
Extract text until second to last occurrence from string

Time:12-14

How do I extract the text from the beginning until (not including) the second to last occurrence of a charachter (":" in this case) from a string? Preferably without using regex.

Examples:

"urn:riv:intygsbestallning:certificate:order:RequestPerformerForAssessmentResponder:1" should become "RequestPerformerForAssessmentResponder:1"

"urn:riv:itinfra:tp:PingResponder:1" should become "PingResponder:1"

CodePudding user response:

let x = "urn:riv:intygsbestallning:certificate:order:RequestPerformerForAssessmentResponder:1";

let result = x.split(":");
let yourTextResult = `${result[result.length-2]}:${result[result.length-1]}`;
console.log(yourTextResult );

CodePudding user response:

const data1 = "urn:riv:intygsbestallning:certificate:order:RequestPerformerForAssessmentResponder:1";
const data2 = "urn:riv:itinfra:tp:PingResponder:1"

const arr = data1.split(':');
console.log(arr.splice(arr.length-2,arr.length).join(':'))

  • Related