How to split a string and keep the whitespace in Node.js

1 Answer

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

const arr = str.split(/(\s+)/);

console.log(arr);

  
  
  
  
/*
run:

[ 'typescript', ' ', 'javascript', ' ', 'python', ' ', 'node.js' ]
  
*/

 



answered Apr 29, 2022 by avibootz
...