【ES9(2018)】RegExp扩展
发布日期:2021-05-13 00:16:26 浏览次数:19 分类:精选文章

本文共 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/s
console.log(re.test('foo\nbar')) // true
console.log(re.dotAll) // true
������ dotAll ������

������ re.flags ��������������� dotAll ������������������������

const re = /foo.bar/s
console.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 ������������������������������

上一篇:【ES9(2018)】Object Rest & Spread
下一篇:【ES9(2018)】for await...of

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月07日 03时12分56秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章