title | tags | |
---|---|---|
How to split strings in JavaScript |
|
'I 💖 U'.split(' '); // ✅: [ 'I', '💖', 'U' ]
'I💖U'.split(''); // ❌: [ 'I', '�', '�', 'U' ]
Better alternatives:
[...'I💖U'];
Array.from('I💖U');
'I💖U'.split(/(?=[\s\S])/u);
More info: MDN, stackoverflow.com
Please note - it's still a bit more complicated. Read this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#reversing_a_string_using_split