How to split a string on the last occurrence of a substring in Node.js

1 Answer

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

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

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

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


  
  
  
  
/*
run:
  
python typescript python javascript 
python node.js

*/

 



answered Apr 29, 2022 by avibootz
...