How to split a string and keep the whitespace in JavaScript

1 Answer

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

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

console.log(arr);

  
  
  
  
/*
run:

["typescript", " ", "javascript", " ", "c++", " ", "node.js"]
  
*/

 



answered Apr 29, 2022 by avibootz
...