
本文共 4326 字,大约阅读时间需要 14 分钟。
ES6 ������������������������������������
������������������ JavaScript ������������������������������������������ES6 ��������������������������������������������������������������������������������������������� y
��� u
���������������������������������������������
1. y
���������
y
������������������������������������������������������������������������������������������������������������������������������ g
������������������y
���������������������������������������������������������������
������������
- ������������������
lastIndex
������������������������������������������������������������������������������������������������ - ���
g
���������������������g
���������������������������������������������������y
���������������������������lastIndex
������������
������������
const s = 'aaa_aa_a';// ������ `g` ���������const r1 = /a+/g;const match1 = r1.exec(s); // ['aaa']// ��������������������������� 'a' ������const match2 = r1.exec(s); // ['aa']// ������ `y` ���������const r2 = /a+/y;const match3 = r2.exec(s); // ['aaa']const match4 = r2.exec(s); // null// ���������������- `g` ���������������������������������������- `y` ��������������������������������������� `lastIndex` ������������������������
������������
������������������������������������������������������������������������������������ y
������������ lastIndex
���������������������������������
const regexp = /a/g;// ������������������������������������������������������������ 1���regexp.lastIndex = 1;// ������������ 1 ���������������������const match = regexp.exec('axxa'); // 'xx'// ��������������������������� 4������������������ 4 ������������������������ null
��� y
������������������������������ lastIndex
���������������������������������������������������������������������������������
2. u
���������
u
���������Unicode������������������������������������ \uFFFF
��� Unicode ������������������������������������������������������������������������������������
������������
- UTF-16 ��������������������������������������������������������������� Unicode ������������������������������ UTF-16 ���������
- ���������������������������������������������������������������������������������������
\S
��������������������������� Unicode ���������
������������
// ������ 1������������������������������const s = '����'; // ������������ Unicode ������������console.log(s.length); // ��������� 4��������� UTF-16���console.log(/^\S$/u.test(s)); // ������ true// ������ 2��������� Unicode ���������console.log(/^\u{61}/.test('a')); // ������ false������������ u ������������console.log(/^\u{61}/u.test('a')); // ������ true
������
3. u
������������������������
1. ���������������
��� u
��������������������������������� .
������������������������������������������������������������������������������������ Unicode ���������
// ��������� `u` ���������������const s = '����';'^.$.test(s); // ������ false������������������������������// ������ `u` ������������'^.$/u.test(s); // ������ true
2. Unicode ���������������
������ u
��������������������������������������������� \u{61}
������������������������ a
���
// ������ `u` ������������console.log(/\\u{61}/u.test('a')); // true
3. ������������
��� u
������������������������ Unicode ������������������������������
// ������������������const s = '��������';console.log(/a{2}/u.test(s)); // ������ true
4. ���������������������
������������������������������������������������ Unicode ���������
// ��������� `u` ������������console.log('����'.match(/\S/gi)); // null// ������ `u` ������������console.log('����'.match(/\S/giu)); // ������ [ '����' ]
4. ������ u
��� y
������������������
������ u
��� y
���������������������������������������������������������������������������������������������UTF-16������������������������������������
const s = '��������';// ���������������������������������������������function codePointLength(text) { const result = text.match(/[\s\S]/gu); return result ? result.length : 0;}console.log(s.length); // 8console.log(codePointLength(s)); // 2
���������������
y
���������������������������������������������lastIndex
���������u
��������������������������������������������������� Unicode ���������y
+u
���������������������������������������������������������������
������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
