How to split a string on the last occurrence of a substring in TypeScript

1 Answer

0 votes
const str = 'php typescript php javascript php node.js';

const lastIndex = str.lastIndexOf("php");

const before = str.slice(0, lastIndex);
console.log(before); 

const after = str.slice(lastIndex);
console.log(after); 

  
  
  
  
/*
run:
  
"php typescript php javascript " 
"php node.js" 
  
*/

 



answered Apr 29, 2022 by avibootz
...