Я поигрался с некоторым кодом и столкнулся со странным поведением. Можете ли вы объяснить мне, почему эти выражения дают разные результаты?
const a = 'abcd';
const b = a.split(/\b|\B/);
console.log('b: ', b);
const c = a.split(/(\b|\B)/);
console.log('c: ', c);
const d = a.split(/(\b|\B){0}/);
console.log('d: ', d);
const e = a.split(/(\b|\B)(?=(\b|\B){0})/);
console.log('e: ', e);
const f = a.split(/(\b|\B){0}(?=(\b|\B){0})/);
console.log('f: ', f);
Вывод:
b: [ 'a', 'b', 'c', 'd' ]
c: [ 'a', '', 'b', '', 'c', '', 'd' ]
d: [ 'a', undefined, 'b', undefined, 'c', undefined, 'd' ]
e: [ 'a', '', undefined, 'b', '', undefined, 'c', '', undefined, 'd' ]
f: [ 'a',
undefined,
undefined,
'b',
undefined,
undefined,
'c',
undefined,
undefined,
'd' ]