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

1 Answer

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

const lastIndex = str.lastIndexOf('.');

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

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

  
  
  
  
/*
run:
  
typescript. javascript. node
js python

*/

 



answered Apr 29, 2022 by avibootz
...