
本文共 1962 字,大约阅读时间需要 6 分钟。
������������������������������
1. dotAll ������
���������������������������.��������������������������������������������������������������������������������������� UTF-8 ������������������������������������ u
���������������������������������������������line terminator character������
������������������
U+000A
������������\n
U+000D
������������\r
U+2028
��������������� (\u{2028}
)U+2029
��������������� (\u{2029}
)
������������
��� ES5 ������������������������ \
���������������
console.log(/foo[^]bar/.test('foo\nbar')) // true���console.log(/foo[\s\S]bar/.test('foo\nbar')) // true
������������������ JavaScript ������������������������ re.flags
������������ s
��������������������� dotAll ���������
const re = /foo.bar/sconsole.log(re.test('foo\nbar')) // trueconsole.log(re.dotAll) // true
������ dotAll ������
������ re.flags
��������������� dotAll ������������������������
const re = /foo.bar/sconsole.log(re.flags) // 's'
2. ���������������
��������������������������������� ()
���������������������������������������������
console.log('2020-05-01'.match(/(\d{4})-(\d{2})-(\d{2})/)) // ���������������������������������������
������������������
index
������������������������input
������������������groups
���������������������undefined
���������������
������ (?<name>...)
������������������������
const regex = /(?\d{4})-(? \d{2})-(? \d{2})/
������
console.log('2020-05-01'.match(/(\d{4})-(\d{2})-(\d{2})/)) // ���������["2020-05-01", "2020", "05", "01"]
3. ������������
��� ES9 ������JavaScript ��������������������������������������� (?=...)
��������������������� (?<=...)
���
������
������������������ world
��� hello
���
const test = 'world hello'console.log(test.match(/(?<=world\s)hello/)) // ������������������
ES9 ������
��� ES9 ������������������������ (?<=...)
���������
console.log('world hello'.match(/(?<=world\s)hello/)) // ������������������
������������ JavaScript ������������������������������
发表评论
最新留言
关于作者
