简体   繁体   中英

javascript how to split a string in multiple arrays after specific character

I am trying to split (in Vanilla JS) a string into a single or multiple arrays after each \n character.

For example:

let string = '1 2 3 4\n5 6 7 8\n9 8 7 6';

should return:

let combinedArrays = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 8, 7, 6]];

by splitting the let string after every \n character and deleting all the space characters.

Thanks for your help.

let combinedArrays = string.split('\n').map(el => el.split(' '))

 let string = '1 2 3 4\n5 6 7 8\n9 8 7 6'; console.log(string.split('\n').map(el => el.split(' ')))

Please read more about split() method here: https://developer.mozilla.org/uk/docs/Web/JavaScript/Reference/Global_Objects/String/split

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM