How to split a string on the last occurrence of a character in JavaScript

1 Answer

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

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 c++"
  
*/

 



answered Apr 29, 2022 by avibootz
...