123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <title>正则的扩展 | 彪哥博客</title>
- <meta name="generator" content="VuePress 1.9.5">
- <link rel="icon" href="/blog/img/favicon.ico">
- <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
- <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
- <meta name="theme-color" content="#11a8cd">
-
- <link rel="preload" href="/blog/assets/css/0.styles.cf8cd190.css" as="style"><link rel="preload" href="/blog/assets/js/app.14d95a24.js" as="script"><link rel="preload" href="/blog/assets/js/2.395c0d18.js" as="script"><link rel="preload" href="/blog/assets/js/3.6748bd5c.js" as="script"><link rel="preload" href="/blog/assets/js/93.c8ee75e3.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cad3aa70.js"><link rel="prefetch" href="/blog/assets/js/100.08a8b2d8.js"><link rel="prefetch" href="/blog/assets/js/101.2aabb12c.js"><link rel="prefetch" href="/blog/assets/js/102.13f3cc4d.js"><link rel="prefetch" href="/blog/assets/js/103.c19aee03.js"><link rel="prefetch" href="/blog/assets/js/104.724d4908.js"><link rel="prefetch" href="/blog/assets/js/105.72966789.js"><link rel="prefetch" href="/blog/assets/js/106.207422de.js"><link rel="prefetch" href="/blog/assets/js/107.bf754f60.js"><link rel="prefetch" href="/blog/assets/js/108.46d6298b.js"><link rel="prefetch" href="/blog/assets/js/109.de6075c6.js"><link rel="prefetch" href="/blog/assets/js/11.f2e9eca8.js"><link rel="prefetch" href="/blog/assets/js/110.f41596b0.js"><link rel="prefetch" href="/blog/assets/js/111.da99a105.js"><link rel="prefetch" href="/blog/assets/js/112.9dd75c6f.js"><link rel="prefetch" href="/blog/assets/js/113.9322f157.js"><link rel="prefetch" href="/blog/assets/js/114.c5150cc6.js"><link rel="prefetch" href="/blog/assets/js/115.f58b49f8.js"><link rel="prefetch" href="/blog/assets/js/116.8f7d9c6f.js"><link rel="prefetch" href="/blog/assets/js/117.3c970f48.js"><link rel="prefetch" href="/blog/assets/js/118.921e1d54.js"><link rel="prefetch" href="/blog/assets/js/119.6ef74e70.js"><link rel="prefetch" href="/blog/assets/js/12.98512c60.js"><link rel="prefetch" href="/blog/assets/js/120.9be3d6fd.js"><link rel="prefetch" href="/blog/assets/js/121.a0b3693a.js"><link rel="prefetch" href="/blog/assets/js/122.6c7dd225.js"><link rel="prefetch" href="/blog/assets/js/123.dbff103c.js"><link rel="prefetch" href="/blog/assets/js/124.493776ef.js"><link rel="prefetch" href="/blog/assets/js/125.554c9fbf.js"><link rel="prefetch" href="/blog/assets/js/126.9d3b75dc.js"><link rel="prefetch" href="/blog/assets/js/127.aad20a7e.js"><link rel="prefetch" href="/blog/assets/js/128.6543adba.js"><link rel="prefetch" href="/blog/assets/js/129.d7c56b92.js"><link rel="prefetch" href="/blog/assets/js/13.a79fa0c7.js"><link rel="prefetch" href="/blog/assets/js/130.593d21f0.js"><link rel="prefetch" href="/blog/assets/js/131.4c90d8b8.js"><link rel="prefetch" href="/blog/assets/js/132.4ad12bdc.js"><link rel="prefetch" href="/blog/assets/js/133.485de1b9.js"><link rel="prefetch" href="/blog/assets/js/134.dc7521fd.js"><link rel="prefetch" href="/blog/assets/js/135.196c8be6.js"><link rel="prefetch" href="/blog/assets/js/136.d99350df.js"><link rel="prefetch" href="/blog/assets/js/137.71e8757e.js"><link rel="prefetch" href="/blog/assets/js/138.75e29c6f.js"><link rel="prefetch" href="/blog/assets/js/139.766f20b7.js"><link rel="prefetch" href="/blog/assets/js/14.0fdf0c78.js"><link rel="prefetch" href="/blog/assets/js/140.0d9b8fbc.js"><link rel="prefetch" href="/blog/assets/js/141.128b6e26.js"><link rel="prefetch" href="/blog/assets/js/142.d7c4dea7.js"><link rel="prefetch" href="/blog/assets/js/143.9aa933f3.js"><link rel="prefetch" href="/blog/assets/js/144.277e558e.js"><link rel="prefetch" href="/blog/assets/js/145.87ec869d.js"><link rel="prefetch" href="/blog/assets/js/146.4185092c.js"><link rel="prefetch" href="/blog/assets/js/147.ac0e55d3.js"><link rel="prefetch" href="/blog/assets/js/148.30f02604.js"><link rel="prefetch" href="/blog/assets/js/149.7a6ca4f7.js"><link rel="prefetch" href="/blog/assets/js/15.fce722b2.js"><link rel="prefetch" href="/blog/assets/js/150.c3450fb8.js"><link rel="prefetch" href="/blog/assets/js/151.07f1a9e9.js"><link rel="prefetch" href="/blog/assets/js/152.0f318f01.js"><link rel="prefetch" href="/blog/assets/js/153.a8608e73.js"><link rel="prefetch" href="/blog/assets/js/154.8ab912b6.js"><link rel="prefetch" href="/blog/assets/js/155.5ee5b308.js"><link rel="prefetch" href="/blog/assets/js/156.4a049fb7.js"><link rel="prefetch" href="/blog/assets/js/157.99ee745c.js"><link rel="prefetch" href="/blog/assets/js/158.09b15f2b.js"><link rel="prefetch" href="/blog/assets/js/159.a83db4c9.js"><link rel="prefetch" href="/blog/assets/js/16.7cf1b239.js"><link rel="prefetch" href="/blog/assets/js/160.4cded216.js"><link rel="prefetch" href="/blog/assets/js/161.9471d56d.js"><link rel="prefetch" href="/blog/assets/js/162.0c32d380.js"><link rel="prefetch" href="/blog/assets/js/163.41d60a5c.js"><link rel="prefetch" href="/blog/assets/js/164.6dbc8ae8.js"><link rel="prefetch" href="/blog/assets/js/165.7e1b67fe.js"><link rel="prefetch" href="/blog/assets/js/166.ca626fb4.js"><link rel="prefetch" href="/blog/assets/js/167.02bee9f4.js"><link rel="prefetch" href="/blog/assets/js/168.330ac31c.js"><link rel="prefetch" href="/blog/assets/js/169.b7e43910.js"><link rel="prefetch" href="/blog/assets/js/17.93f492a1.js"><link rel="prefetch" href="/blog/assets/js/170.70ecb003.js"><link rel="prefetch" href="/blog/assets/js/171.8828622b.js"><link rel="prefetch" href="/blog/assets/js/172.b659d767.js"><link rel="prefetch" href="/blog/assets/js/173.1b915b91.js"><link rel="prefetch" href="/blog/assets/js/174.307c1d13.js"><link rel="prefetch" href="/blog/assets/js/175.44316a23.js"><link rel="prefetch" href="/blog/assets/js/176.044e8f54.js"><link rel="prefetch" href="/blog/assets/js/177.fae8b8e2.js"><link rel="prefetch" href="/blog/assets/js/178.b6e40e29.js"><link rel="prefetch" href="/blog/assets/js/179.c69ea8d8.js"><link rel="prefetch" href="/blog/assets/js/18.f17de23a.js"><link rel="prefetch" href="/blog/assets/js/180.955aa8ec.js"><link rel="prefetch" href="/blog/assets/js/181.a53e32e0.js"><link rel="prefetch" href="/blog/assets/js/182.38687994.js"><link rel="prefetch" href="/blog/assets/js/183.544fef00.js"><link rel="prefetch" href="/blog/assets/js/184.ae09b86e.js"><link rel="prefetch" href="/blog/assets/js/185.2f5f6e20.js"><link rel="prefetch" href="/blog/assets/js/186.0efe164b.js"><link rel="prefetch" href="/blog/assets/js/187.df0138d8.js"><link rel="prefetch" href="/blog/assets/js/188.5d767d7a.js"><link rel="prefetch" href="/blog/assets/js/189.137e022f.js"><link rel="prefetch" href="/blog/assets/js/19.6b963460.js"><link rel="prefetch" href="/blog/assets/js/190.5270cf9b.js"><link rel="prefetch" href="/blog/assets/js/191.862607b0.js"><link rel="prefetch" href="/blog/assets/js/192.127fef4c.js"><link rel="prefetch" href="/blog/assets/js/193.781690eb.js"><link rel="prefetch" href="/blog/assets/js/194.1e80b1b8.js"><link rel="prefetch" href="/blog/assets/js/195.003e3d67.js"><link rel="prefetch" href="/blog/assets/js/196.7a3f55e5.js"><link rel="prefetch" href="/blog/assets/js/197.30d4c5b4.js"><link rel="prefetch" href="/blog/assets/js/198.628c2c1a.js"><link rel="prefetch" href="/blog/assets/js/199.89c4e586.js"><link rel="prefetch" href="/blog/assets/js/20.839dae41.js"><link rel="prefetch" href="/blog/assets/js/200.1d9f2ef9.js"><link rel="prefetch" href="/blog/assets/js/201.5ba078d9.js"><link rel="prefetch" href="/blog/assets/js/202.b49b23f4.js"><link rel="prefetch" href="/blog/assets/js/203.d88a03d9.js"><link rel="prefetch" href="/blog/assets/js/204.fb928277.js"><link rel="prefetch" href="/blog/assets/js/205.432c3d8d.js"><link rel="prefetch" href="/blog/assets/js/206.ed726599.js"><link rel="prefetch" href="/blog/assets/js/207.9ac30d7b.js"><link rel="prefetch" href="/blog/assets/js/208.c5e77adc.js"><link rel="prefetch" href="/blog/assets/js/209.d396aad5.js"><link rel="prefetch" href="/blog/assets/js/21.bd21bd29.js"><link rel="prefetch" href="/blog/assets/js/210.02e4ee2f.js"><link rel="prefetch" href="/blog/assets/js/211.0f8a9304.js"><link rel="prefetch" href="/blog/assets/js/212.8a476248.js"><link rel="prefetch" href="/blog/assets/js/213.aab2ac5c.js"><link rel="prefetch" href="/blog/assets/js/214.cd5ec468.js"><link rel="prefetch" href="/blog/assets/js/215.0084d772.js"><link rel="prefetch" href="/blog/assets/js/216.ebd468b9.js"><link rel="prefetch" href="/blog/assets/js/217.fac21407.js"><link rel="prefetch" href="/blog/assets/js/218.9793e19a.js"><link rel="prefetch" href="/blog/assets/js/219.c89175e9.js"><link rel="prefetch" href="/blog/assets/js/22.e6bcf65d.js"><link rel="prefetch" href="/blog/assets/js/220.d496e411.js"><link rel="prefetch" href="/blog/assets/js/221.07a9338a.js"><link rel="prefetch" href="/blog/assets/js/222.22bf261f.js"><link rel="prefetch" href="/blog/assets/js/223.b405119d.js"><link rel="prefetch" href="/blog/assets/js/224.364b4b11.js"><link rel="prefetch" href="/blog/assets/js/225.adec6660.js"><link rel="prefetch" href="/blog/assets/js/226.d7ab4163.js"><link rel="prefetch" href="/blog/assets/js/227.da4481b8.js"><link rel="prefetch" href="/blog/assets/js/228.7243da66.js"><link rel="prefetch" href="/blog/assets/js/229.bf1f474d.js"><link rel="prefetch" href="/blog/assets/js/23.7bb82fc7.js"><link rel="prefetch" href="/blog/assets/js/230.109a1752.js"><link rel="prefetch" href="/blog/assets/js/231.b1c6db75.js"><link rel="prefetch" href="/blog/assets/js/232.176c4df4.js"><link rel="prefetch" href="/blog/assets/js/233.2c81f1b3.js"><link rel="prefetch" href="/blog/assets/js/234.bb73adca.js"><link rel="prefetch" href="/blog/assets/js/235.97ed69db.js"><link rel="prefetch" href="/blog/assets/js/236.a8c51930.js"><link rel="prefetch" href="/blog/assets/js/237.6ae31c88.js"><link rel="prefetch" href="/blog/assets/js/238.2f5c56ae.js"><link rel="prefetch" href="/blog/assets/js/239.506a4e9f.js"><link rel="prefetch" href="/blog/assets/js/24.2c9c4ee6.js"><link rel="prefetch" href="/blog/assets/js/25.b4de33d1.js"><link rel="prefetch" href="/blog/assets/js/26.0bb98ba9.js"><link rel="prefetch" href="/blog/assets/js/27.df98327e.js"><link rel="prefetch" href="/blog/assets/js/28.31289bac.js"><link rel="prefetch" href="/blog/assets/js/29.45af5621.js"><link rel="prefetch" href="/blog/assets/js/30.d5c08e66.js"><link rel="prefetch" href="/blog/assets/js/31.78e43a68.js"><link rel="prefetch" href="/blog/assets/js/32.53ca76ee.js"><link rel="prefetch" href="/blog/assets/js/33.081e8ef6.js"><link rel="prefetch" href="/blog/assets/js/34.cb1866c1.js"><link rel="prefetch" href="/blog/assets/js/35.dca9b927.js"><link rel="prefetch" href="/blog/assets/js/36.fb4476c3.js"><link rel="prefetch" href="/blog/assets/js/37.09dfc1c5.js"><link rel="prefetch" href="/blog/assets/js/38.6624bf02.js"><link rel="prefetch" href="/blog/assets/js/39.df6c26ac.js"><link rel="prefetch" href="/blog/assets/js/4.44654b1a.js"><link rel="prefetch" href="/blog/assets/js/40.80101c19.js"><link rel="prefetch" href="/blog/assets/js/41.2b5e8c27.js"><link rel="prefetch" href="/blog/assets/js/42.c6ded3fe.js"><link rel="prefetch" href="/blog/assets/js/43.6d9424d6.js"><link rel="prefetch" href="/blog/assets/js/44.835e4b5c.js"><link rel="prefetch" href="/blog/assets/js/45.d74d29a2.js"><link rel="prefetch" href="/blog/assets/js/46.d15a7dc0.js"><link rel="prefetch" href="/blog/assets/js/47.8d66ca97.js"><link rel="prefetch" href="/blog/assets/js/48.3c1102e1.js"><link rel="prefetch" href="/blog/assets/js/49.e17a3436.js"><link rel="prefetch" href="/blog/assets/js/5.88de390f.js"><link rel="prefetch" href="/blog/assets/js/50.6750f186.js"><link rel="prefetch" href="/blog/assets/js/51.9f93af9f.js"><link rel="prefetch" href="/blog/assets/js/52.f3ef3b5e.js"><link rel="prefetch" href="/blog/assets/js/53.a6bacd25.js"><link rel="prefetch" href="/blog/assets/js/54.dbb7c9ab.js"><link rel="prefetch" href="/blog/assets/js/55.2562d0c8.js"><link rel="prefetch" href="/blog/assets/js/56.14ea4931.js"><link rel="prefetch" href="/blog/assets/js/57.a2fad780.js"><link rel="prefetch" href="/blog/assets/js/58.8165b971.js"><link rel="prefetch" href="/blog/assets/js/59.556cab0d.js"><link rel="prefetch" href="/blog/assets/js/6.277038ca.js"><link rel="prefetch" href="/blog/assets/js/60.e2a6ab4c.js"><link rel="prefetch" href="/blog/assets/js/61.16bf38a2.js"><link rel="prefetch" href="/blog/assets/js/62.37a94f10.js"><link rel="prefetch" href="/blog/assets/js/63.74811780.js"><link rel="prefetch" href="/blog/assets/js/64.81f21b8a.js"><link rel="prefetch" href="/blog/assets/js/65.d970ff03.js"><link rel="prefetch" href="/blog/assets/js/66.cb805d9b.js"><link rel="prefetch" href="/blog/assets/js/67.39f85baa.js"><link rel="prefetch" href="/blog/assets/js/68.7f79766a.js"><link rel="prefetch" href="/blog/assets/js/69.fa8624bd.js"><link rel="prefetch" href="/blog/assets/js/7.e0a6d1b0.js"><link rel="prefetch" href="/blog/assets/js/70.1f3e978d.js"><link rel="prefetch" href="/blog/assets/js/71.13cd9358.js"><link rel="prefetch" href="/blog/assets/js/72.739b22a8.js"><link rel="prefetch" href="/blog/assets/js/73.03af83b4.js"><link rel="prefetch" href="/blog/assets/js/74.f23e183c.js"><link rel="prefetch" href="/blog/assets/js/75.b8d21d41.js"><link rel="prefetch" href="/blog/assets/js/76.fd0f1cda.js"><link rel="prefetch" href="/blog/assets/js/77.46f6e413.js"><link rel="prefetch" href="/blog/assets/js/78.aebd00ee.js"><link rel="prefetch" href="/blog/assets/js/79.ad91baa5.js"><link rel="prefetch" href="/blog/assets/js/8.9428e7ee.js"><link rel="prefetch" href="/blog/assets/js/80.593a99e8.js"><link rel="prefetch" href="/blog/assets/js/81.101cc131.js"><link rel="prefetch" href="/blog/assets/js/82.077c8298.js"><link rel="prefetch" href="/blog/assets/js/83.2e375d11.js"><link rel="prefetch" href="/blog/assets/js/84.38102a34.js"><link rel="prefetch" href="/blog/assets/js/85.24532d6a.js"><link rel="prefetch" href="/blog/assets/js/86.1dabbf00.js"><link rel="prefetch" href="/blog/assets/js/87.763da0f2.js"><link rel="prefetch" href="/blog/assets/js/88.ff6e5f7c.js"><link rel="prefetch" href="/blog/assets/js/89.187e5e16.js"><link rel="prefetch" href="/blog/assets/js/9.da143545.js"><link rel="prefetch" href="/blog/assets/js/90.3c8cff94.js"><link rel="prefetch" href="/blog/assets/js/91.faacc3e9.js"><link rel="prefetch" href="/blog/assets/js/92.873706ba.js"><link rel="prefetch" href="/blog/assets/js/94.b18a3e9b.js"><link rel="prefetch" href="/blog/assets/js/95.cddef6ae.js"><link rel="prefetch" href="/blog/assets/js/96.80e5a938.js"><link rel="prefetch" href="/blog/assets/js/97.1f5e5197.js"><link rel="prefetch" href="/blog/assets/js/98.e3a275c8.js"><link rel="prefetch" href="/blog/assets/js/99.d33bf89e.js">
- <link rel="stylesheet" href="/blog/assets/css/0.styles.cf8cd190.css">
- </head>
- <body class="theme-mode-light">
- <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="彪哥博客" class="logo"> <span class="site-name can-hide">彪哥博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
- 个人游戏网站
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
- GitHub
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="/img/head.jpg"> <div class="blogger-info"><h3>彪哥</h3> <span>爱好前端</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
- 个人游戏网站
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
- GitHub
- <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><a href="/blog/pages/f344d070a1031ef7/" class="sidebar-link">ECMAScript 6 简介</a></li><li><a href="/blog/pages/c1edd70a6b7c7872/" class="sidebar-link">let 和 const 命令</a></li><li><a href="/blog/pages/b1ab10a62f7564da/" class="sidebar-link">变量的解构赋值</a></li><li><a href="/blog/pages/ca89eca8adeba5f4/" class="sidebar-link">字符串的扩展</a></li><li><a href="/blog/pages/a650b4a0ebfc9350/" class="sidebar-link">字符串的新增方法</a></li><li><a href="/blog/pages/0473261a6ab0ee8c/" aria-current="page" class="active sidebar-link">正则的扩展</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-构造函数" class="sidebar-link">RegExp 构造函数</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#字符串的正则方法" class="sidebar-link">字符串的正则方法</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#u-修饰符" class="sidebar-link">u 修饰符</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-prototype-unicode-属性" class="sidebar-link">RegExp.prototype.unicode 属性</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#y-修饰符" class="sidebar-link">y 修饰符</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-prototype-sticky-属性" class="sidebar-link">RegExp.prototype.sticky 属性</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#regexp-prototype-flags-属性" class="sidebar-link">RegExp.prototype.flags 属性</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#s-修饰符-dotall-模式" class="sidebar-link">s 修饰符:dotAll 模式</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#后行断言" class="sidebar-link">后行断言</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#unicode-属性类" class="sidebar-link">Unicode 属性类</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#具名组匹配" class="sidebar-link">具名组匹配</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/0473261a6ab0ee8c/#简介" class="sidebar-link">简介</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/0473261a6ab0ee8c/#解构赋值和替换" class="sidebar-link">解构赋值和替换</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/0473261a6ab0ee8c/#引用" class="sidebar-link">引用</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/0473261a6ab0ee8c/#string-prototype-matchall" class="sidebar-link">String.prototype.matchAll()</a></li></ul></li><li><a href="/blog/pages/5dfea9a0f2d1a392/" class="sidebar-link">数值的扩展</a></li><li><a href="/blog/pages/8ed309d668b20264/" class="sidebar-link">函数的扩展</a></li><li><a href="/blog/pages/e34009d60d8bc4b2/" class="sidebar-link">数组的扩展</a></li><li><a href="/blog/pages/b5e3e0a0ff6e9c25/" class="sidebar-link">对象的扩展</a></li><li><a href="/blog/pages/e85e68947502cf90/" class="sidebar-link">对象的新增方法</a></li><li><a href="/blog/pages/02c86eb2792f3262/" class="sidebar-link">Symbol</a></li><li><a href="/blog/pages/0c21dae358fca16b/" class="sidebar-link">Set 和 Map 数据结构</a></li><li><a href="/blog/pages/f56ec2ab97d60483/" class="sidebar-link">Proxy</a></li><li><a href="/blog/pages/74de3e45e4491e95/" class="sidebar-link">Reflect</a></li><li><a href="/blog/pages/2810ae8985e9bd52/" class="sidebar-link">Promise 对象</a></li><li><a href="/blog/pages/48df907ad3570f3d/" class="sidebar-link">Iterator 和 for-of 循环</a></li><li><a href="/blog/pages/718b48ed9ce0adce/" class="sidebar-link">Generator 函数的语法</a></li><li><a href="/blog/pages/75af7031eb66847b/" class="sidebar-link">Generator 函数的异步应用</a></li><li><a href="/blog/pages/3777253e65bac487/" class="sidebar-link">async 函数</a></li><li><a href="/blog/pages/e831e1593c82bbe0/" class="sidebar-link">Class 的基本语法</a></li><li><a href="/blog/pages/83f8c3a0cd87dd83/" class="sidebar-link">Class 的继承</a></li><li><a href="/blog/pages/efe2fb04eb8ac5fb/" class="sidebar-link">Module 的语法</a></li><li><a href="/blog/pages/a79ca2e64ceae213/" class="sidebar-link">Module 的加载实现</a></li><li><a href="/blog/pages/984bf549204bb266/" class="sidebar-link">编程风格</a></li><li><a href="/blog/pages/32c35f7651d6e58e/" class="sidebar-link">读懂 ECMAScript 规格</a></li><li><a href="/blog/pages/16121351be68691b/" class="sidebar-link">异步遍历器</a></li><li><a href="/blog/pages/a2ba314746bfdbdd/" class="sidebar-link">ArrayBuffer</a></li><li><a href="/blog/pages/7188882b8d65af1b/" class="sidebar-link">最新提案</a></li><li><a href="/blog/pages/e97bc1e5626b082c/" class="sidebar-link">装饰器</a></li><li><a href="/blog/pages/1cf50330655efc69/" class="sidebar-link">函数式编程</a></li><li><a href="/blog/pages/6a8e2dc558da1b39/" class="sidebar-link">Mixin</a></li><li><a href="/blog/pages/8e8f80f69b775a56/" class="sidebar-link">SIMD</a></li><li><a href="/blog/pages/ea6f3b870f6dab69/" class="sidebar-link">参考链接</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06970110><div class="articleInfo" data-v-06970110><ul class="breadcrumbs" data-v-06970110><li data-v-06970110><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-06970110></a></li> <li data-v-06970110><a href="/blog/note/es6/#《ES6 教程》笔记" data-v-06970110>《ES6 教程》笔记</a></li></ul> <div class="info" data-v-06970110><div title="作者" class="author iconfont icon-touxiang" data-v-06970110><a href="javascript:;" data-v-06970110>阮一峰</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06970110><a href="javascript:;" data-v-06970110>2020-02-09</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABGpJREFUSA3tVVtoXFUU3fvOI53UlmCaKIFmwEhsE7QK0ipFEdHEKpXaZGrp15SINsXUWvBDpBgQRKi0+KKoFeJHfZA+ED9KKoIU2gYD9UejTW4rVIzm0VSTziPzuNu1z507dibTTjL4U/DAzLn3nL3X2o91ziX6f9wMFdh6Jvbm9nNSV0msViVO6tN1Rm7NMu2OpeJ9lWBUTDxrJbYTS0hInuwciu9eLHlFxCLCZEk3MegsJmZ5K/JD6t7FkFdEvGUo1g7qJoG3MHImqRIn8/nzY1K9UPKKiJmtnUqHVE3Gbuay6vJE/N2FEmuxFjW2nUuE0yQXRRxLiTUAzs36zhZvOXJPdX850EVnnLZkB8prodQoM5JGj7Xk2mvC7JB8tG04Ef5PiXtG0UtxupRQSfTnBoCy554x18yJHI6I+G5Eru4LHmPJZEQsrvPUbMiA8G/WgMK7w7I+ez7++o2ANfbrjvaOl1tFMs+htG3IrZH9/hDX1Pr8Tc0UvH8tcX29KzAgIGcEkINyW5BF9x891hw6VYqgJHEk0huccS7vh3C6gTiODL+26huuBtbct8eZnqLML8PkxGYpuPZBqtqwkSjgc4mB5gbgig5i+y0UDK35LMxXisn9xQtK+nd26gTIHsHe/oblK/b29fUmN/8Y+9jAQrnBp56m1LcDlDp9irKTExSKduXJVWSqdBMA08pEJnEIOB3FPPMybu/oeV8zFeYN3xx576Q6RH+VmplE4ncQV5v+5rzSoyOU7PuEAg8g803PwBJ0CExno/jcMbN8tONYeOmHiuUNryvm3fRUy4tMPVLdAGkUhNWuggGrJcXPv+ouCjz0MKUHz1J2/E8IC9nqTabcxgaBYM0hPhD5Y65FsbxRQKxCQrDjDctW7PUM3HuZunFyifSAqEfuzCp48Il24luWUWZoyJCaPR82jE0+kFA643wRFVni4RYSq3ohJO2pZ7B5dO4xkDWbEpossJPLSrPjYID8rS2UHTlvyNxqIGsg674XJJ7vnh5L7PNwC4hh2sjCI96mzszOTpxLF0T7l88Yz7lAuK6OnL8gXLOnTvpzSb22YG8W7us3jSebFHeeqnXRG1vt+MoUM84LQIBmMsCTAcOauTh0T0l0neQK7m2bLMt2mGxU3HYssS0J2cdv5wljlPsrIuZLAG/2DOZIXgCYT8uMGZN+e2kSirfxZOPCsC0f24nTZzspnVn9VePS1Z5vubmAGGXG8ZFno9Hel0yfA5ZPhF7Dh972BQJ2qCpgH67lmWtBYbvk6sz02wjky2vXyz0XErP/kFB619js1BtwfOV4OPRqOQBjy3Qbk18vigUPPSD5ceHnwck7W9bhAqZdd7SuG7w4/P2F/GaJh8c7e9qgow+Q7cGBo+98WsLkuktFqiZabtXuQTu/Y5ETbR0v7tNSFnvrmu6pjdoan2KjMu8q/Hmj1EfCO2ZGfEIbIXKUlw8qaX9/b2oeSJmFksSeT/Fn0V3nSypChh4Gjh74ybO9aeZ/AN2dwciu2/MhAAAAAElFTkSuQmCC">正则的扩展<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="正则的扩展"><a href="#正则的扩展" class="header-anchor">#</a> 正则的扩展</h1> <h2 id="regexp-构造函数"><a href="#regexp-构造函数" class="header-anchor">#</a> RegExp 构造函数</h2> <p>在 ES5 中,<code>RegExp</code>构造函数的参数有两种情况。</p> <p>第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag)。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'xyz'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// 等价于</span>
- <span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div> <p>第二种情况是,参数是一个正则表示式,这时会返回一个原有正则表达式的拷贝。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// 等价于</span>
- <span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>但是,ES5 不允许此时使用第二个参数添加修饰符,否则会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// Uncaught TypeError: Cannot supply flags when constructing one RegExp from another</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>ES6 改变了这种行为。如果<code>RegExp</code>构造函数第一个参数是一个正则对象,那么可以使用第二个参数指定修饰符。而且,返回的正则表达式会忽略原有的正则表达式的修饰符,只使用新指定的修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc</span><span class="token regex-delimiter">/</span><span class="token regex-flags">ig</span></span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>flags
- <span class="token comment">// "i"</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,原有正则对象的修饰符是<code>ig</code>,它会被第二个参数<code>i</code>覆盖。</p> <h2 id="字符串的正则方法"><a href="#字符串的正则方法" class="header-anchor">#</a> 字符串的正则方法</h2> <p>字符串对象共有 4 个方法,可以使用正则表达式:<code>match()</code>、<code>replace()</code>、<code>search()</code>和<code>split()</code>。</p> <p>ES6 将这 4 个方法,在语言内部全部调用<code>RegExp</code>的实例方法,从而做到所有与正则相关的方法,全都定义在<code>RegExp</code>对象上。</p> <ul><li><code>String.prototype.match</code> 调用 <code>RegExp.prototype[Symbol.match]</code></li> <li><code>String.prototype.replace</code> 调用 <code>RegExp.prototype[Symbol.replace]</code></li> <li><code>String.prototype.search</code> 调用 <code>RegExp.prototype[Symbol.search]</code></li> <li><code>String.prototype.split</code> 调用 <code>RegExp.prototype[Symbol.split]</code></li></ul> <h2 id="u-修饰符"><a href="#u-修饰符" class="header-anchor">#</a> u 修饰符</h2> <p>ES6 对正则表达式添加了<code>u</code>修饰符,含义为“Unicode 模式”,用来正确处理大于<code>\uFFFF</code>的 Unicode 字符。也就是说,会正确处理四个字节的 UTF-16 编码。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\uD83D</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\uD83D\uDC2A'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- <span class="token operator">/</span><span class="token operator">^</span>\uD83D<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\uD83D\uDC2A'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>\uD83D\uDC2A</code>是一个四个字节的 UTF-16 编码,代表一个字符。但是,ES5 不支持四个字节的 UTF-16 编码,会将其识别为两个字符,导致第二行代码结果为<code>true</code>。加了<code>u</code>修饰符以后,ES6 就会识别其为一个字符,所以第一行代码结果为<code>false</code>。</p> <p>一旦加上<code>u</code>修饰符号,就会修改下面这些正则表达式的行为。</p> <p><strong>(1)点字符</strong></p> <p>点(<code>.</code>)字符在正则表达式中,含义是除了换行符以外的任意单个字符。对于码点大于<code>0xFFFF</code>的 Unicode 字符,点字符不能识别,必须加上<code>u</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'𠮷'</span><span class="token punctuation">;</span>
- <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^.$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// false</span>
- <span class="token operator">/</span><span class="token operator">^</span><span class="token punctuation">.</span>$<span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码表示,如果不添加<code>u</code>修饰符,正则表达式就会认为字符串为两个字符,从而匹配失败。</p> <p><strong>(2)Unicode 字符表示法</strong></p> <p>ES6 新增了使用大括号表示 Unicode 字符,这种表示法在正则表达式中必须加上<code>u</code>修饰符,才能识别当中的大括号,否则会被解读为量词。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\u{61}</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- <span class="token operator">/</span>\u<span class="token punctuation">{</span><span class="token number">61</span><span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- <span class="token operator">/</span>\u<span class="token punctuation">{</span>20BB7<span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码表示,如果不加<code>u</code>修饰符,正则表达式无法识别<code>\u{61}</code>这种表示法,只会认为这匹配 61 个连续的<code>u</code>。</p> <p><strong>(3)量词</strong></p> <p>使用<code>u</code>修饰符后,所有量词都会正确识别码点大于<code>0xFFFF</code>的 Unicode 字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a{2}</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'aa'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- <span class="token operator">/</span>a<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'aa'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- <span class="token operator">/</span>𠮷<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- <span class="token operator">/</span>𠮷<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>(4)预定义模式</strong></p> <p><code>u</code>修饰符也影响到预定义模式,能否正确识别码点大于<code>0xFFFF</code>的 Unicode 字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\S$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- <span class="token operator">/</span><span class="token operator">^</span>\<span class="token constant">S</span>$<span class="token operator">/</span>u<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𠮷'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码的<code>\S</code>是预定义模式,匹配所有非空白字符。只有加了<code>u</code>修饰符,它才能正确匹配码点大于<code>0xFFFF</code>的 Unicode 字符。</p> <p>利用这一点,可以写出一个正确返回字符串长度的函数。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">codePointLength</span><span class="token punctuation">(</span><span class="token parameter">text</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">var</span> result <span class="token operator">=</span> text<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\s\S]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gu</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token keyword">return</span> result <span class="token operator">?</span> result<span class="token punctuation">.</span>length <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'𠮷𠮷'</span><span class="token punctuation">;</span>
- s<span class="token punctuation">.</span>length <span class="token comment">// 4</span>
- <span class="token function">codePointLength</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// 2</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>(5)i 修饰符</strong></p> <p>有些 Unicode 字符的编码不同,但是字型很相近,比如,<code>\u004B</code>与<code>\u212A</code>都是大写的<code>K</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[a-z]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\u212A'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- <span class="token operator">/</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">/</span>iu<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\u212A'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,不加<code>u</code>修饰符,就无法识别非规范的<code>K</code>字符。</p> <p><strong>(6)转义</strong></p> <p>没有<code>u</code>修饰符的情况下,正则中没有定义的转义(如逗号的转义<code>\,</code>)无效,而在<code>u</code>模式会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\,</span><span class="token regex-delimiter">/</span></span> <span class="token comment">// /\,/</span>
- <span class="token operator">/</span>\<span class="token punctuation">,</span><span class="token operator">/</span>u <span class="token comment">// 报错</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,没有<code>u</code>修饰符时,逗号前面的反斜杠是无效的,加了<code>u</code>修饰符就报错。</p> <h2 id="regexp-prototype-unicode-属性"><a href="#regexp-prototype-unicode-属性" class="header-anchor">#</a> RegExp.prototype.unicode 属性</h2> <p>正则实例对象新增<code>unicode</code>属性,表示是否设置了<code>u</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> r1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">hello</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> r2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">hello</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
- r1<span class="token punctuation">.</span>unicode <span class="token comment">// false</span>
- r2<span class="token punctuation">.</span>unicode <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,正则表达式是否设置了<code>u</code>修饰符,可以从<code>unicode</code>属性看出来。</p> <h2 id="y-修饰符"><a href="#y-修饰符" class="header-anchor">#</a> y 修饰符</h2> <p>除了<code>u</code>修饰符,ES6 还为正则表达式添加了<code>y</code>修饰符,叫做“粘连”(sticky)修饰符。</p> <p><code>y</code>修饰符的作用与<code>g</code>修饰符类似,也是全局匹配,后一次匹配都从上一次匹配成功的下一个位置开始。不同之处在于,<code>g</code>修饰符只要剩余位置中存在匹配就可,而<code>y</code>修饰符确保匹配必须从剩余的第一个位置开始,这也就是“粘连”的涵义。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'aaa_aa_a'</span><span class="token punctuation">;</span>
- <span class="token keyword">var</span> r1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
- <span class="token keyword">var</span> r2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
- r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// ["aaa"]</span>
- r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// ["aaa"]</span>
- r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// ["aa"]</span>
- r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// null</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码有两个正则表达式,一个使用<code>g</code>修饰符,另一个使用<code>y</code>修饰符。这两个正则表达式各执行了两次,第一次执行的时候,两者行为相同,剩余字符串都是<code>_aa_a</code>。由于<code>g</code>修饰没有位置要求,所以第二次执行会返回结果,而<code>y</code>修饰符要求匹配必须从头部开始,所以返回<code>null</code>。</p> <p>如果改一下正则表达式,保证每次都能头部匹配,<code>y</code>修饰符就会返回结果了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'aaa_aa_a'</span><span class="token punctuation">;</span>
- <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+_</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
- r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// ["aaa_"]</span>
- r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// ["aa_"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码每次匹配,都是从剩余字符串的头部开始。</p> <p>使用<code>lastIndex</code>属性,可以更好地说明<code>y</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">REGEX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
- <span class="token comment">// 指定从2号位置(y)开始匹配</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
- <span class="token comment">// 匹配成功</span>
- <span class="token keyword">const</span> match <span class="token operator">=</span> <span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// 在3号位置匹配成功</span>
- match<span class="token punctuation">.</span>index <span class="token comment">// 3</span>
- <span class="token comment">// 下一次匹配从4号位开始</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token comment">// 4</span>
- <span class="token comment">// 4号位开始匹配失败</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中,<code>lastIndex</code>属性指定每次搜索的开始位置,<code>g</code>修饰符从这个位置开始向后搜索,直到发现匹配为止。</p> <p><code>y</code>修饰符同样遵守<code>lastIndex</code>属性,但是要求必须在<code>lastIndex</code>指定的位置发现匹配。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">REGEX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
- <span class="token comment">// 指定从2号位置开始匹配</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
- <span class="token comment">// 不是粘连,匹配失败</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>
- <span class="token comment">// 指定从3号位置开始匹配</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
- <span class="token comment">// 3号位置是粘连,匹配成功</span>
- <span class="token keyword">const</span> match <span class="token operator">=</span> <span class="token constant">REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'xaya'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- match<span class="token punctuation">.</span>index <span class="token comment">// 3</span>
- <span class="token constant">REGEX</span><span class="token punctuation">.</span>lastIndex <span class="token comment">// 4</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>实际上,<code>y</code>修饰符号隐含了头部匹配的标志<code>^</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'aba'</span><span class="token punctuation">)</span>
- <span class="token comment">// null</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码由于不能保证头部匹配,所以返回<code>null</code>。<code>y</code>修饰符的设计本意,就是让头部匹配的标志<code>^</code>在全局匹配中都有效。</p> <p>下面是字符串对象的<code>replace</code>方法的例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">REGEX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gy</span></span><span class="token punctuation">;</span>
- <span class="token string">'aaxa'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token constant">REGEX</span><span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span> <span class="token comment">// '--xa'</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,最后一个<code>a</code>因为不是出现在下一次匹配的头部,所以不会被替换。</p> <p>单单一个<code>y</code>修饰符对<code>match</code>方法,只能返回第一个匹配,必须与<code>g</code>修饰符联用,才能返回所有匹配。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token string">'a1a2a3'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a\d</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">)</span> <span class="token comment">// ["a1"]</span>
- <span class="token string">'a1a2a3'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a\d</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gy</span></span><span class="token punctuation">)</span> <span class="token comment">// ["a1", "a2", "a3"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>y</code>修饰符的一个应用,是从字符串提取 token(词元),<code>y</code>修饰符确保了匹配之间不会有漏掉的字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">TOKEN_Y</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\s*(\+|[0-9]+)\s*</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> <span class="token constant">TOKEN_G</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\s*(\+|[0-9]+)\s*</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
- <span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_Y</span><span class="token punctuation">,</span> <span class="token string">'3 + 4'</span><span class="token punctuation">)</span>
- <span class="token comment">// [ '3', '+', '4' ]</span>
- <span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_G</span><span class="token punctuation">,</span> <span class="token string">'3 + 4'</span><span class="token punctuation">)</span>
- <span class="token comment">// [ '3', '+', '4' ]</span>
- <span class="token keyword">function</span> <span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token parameter"><span class="token constant">TOKEN_REGEX</span><span class="token punctuation">,</span> str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
- <span class="token keyword">let</span> match<span class="token punctuation">;</span>
- <span class="token keyword">while</span> <span class="token punctuation">(</span>match <span class="token operator">=</span> <span class="token constant">TOKEN_REGEX</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- result<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>match<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token keyword">return</span> result<span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中,如果字符串里面没有非法字符,<code>y</code>修饰符与<code>g</code>修饰符的提取结果是一样的。但是,一旦出现非法字符,两者的行为就不一样了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_Y</span><span class="token punctuation">,</span> <span class="token string">'3x + 4'</span><span class="token punctuation">)</span>
- <span class="token comment">// [ '3' ]</span>
- <span class="token function">tokenize</span><span class="token punctuation">(</span><span class="token constant">TOKEN_G</span><span class="token punctuation">,</span> <span class="token string">'3x + 4'</span><span class="token punctuation">)</span>
- <span class="token comment">// [ '3', '+', '4' ]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>g</code>修饰符会忽略非法字符,而<code>y</code>修饰符不会,这样就很容易发现错误。</p> <h2 id="regexp-prototype-sticky-属性"><a href="#regexp-prototype-sticky-属性" class="header-anchor">#</a> RegExp.prototype.sticky 属性</h2> <p>与<code>y</code>修饰符相匹配,ES6 的正则实例对象多了<code>sticky</code>属性,表示是否设置了<code>y</code>修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">hello\d</span><span class="token regex-delimiter">/</span><span class="token regex-flags">y</span></span><span class="token punctuation">;</span>
- r<span class="token punctuation">.</span>sticky <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="regexp-prototype-flags-属性"><a href="#regexp-prototype-flags-属性" class="header-anchor">#</a> RegExp.prototype.flags 属性</h2> <p>ES6 为正则表达式新增了<code>flags</code>属性,会返回正则表达式的修饰符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// ES5 的 source 属性</span>
- <span class="token comment">// 返回正则表达式的正文</span>
- <span class="token operator">/</span>abc<span class="token operator">/</span>ig<span class="token punctuation">.</span>source
- <span class="token comment">// "abc"</span>
- <span class="token comment">// ES6 的 flags 属性</span>
- <span class="token comment">// 返回正则表达式的修饰符</span>
- <span class="token operator">/</span>abc<span class="token operator">/</span>ig<span class="token punctuation">.</span>flags
- <span class="token comment">// 'gi'</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="s-修饰符-dotall-模式"><a href="#s-修饰符-dotall-模式" class="header-anchor">#</a> s 修饰符:dotAll 模式</h2> <p>正则表达式中,点(<code>.</code>)是一个特殊字符,代表任意的单个字符,但是有两个例外。一个是四个字节的 UTF-16 字符,这个可以用<code>u</code>修饰符解决;另一个是行终止符(line terminator character)。</p> <p>所谓行终止符,就是该字符表示一行的终结。以下四个字符属于“行终止符”。</p> <ul><li>U+000A 换行符(<code>\n</code>)</li> <li>U+000D 回车符(<code>\r</code>)</li> <li>U+2028 行分隔符(line separator)</li> <li>U+2029 段分隔符(paragraph separator)</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo.bar</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span>
- <span class="token comment">// false</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,因为<code>.</code>不匹配<code>\n</code>,所以正则表达式返回<code>false</code>。</p> <p>但是,很多时候我们希望匹配的是任意单个字符,这时有一种变通的写法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo[^]bar</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span>
- <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>这种解决方案毕竟不太符合直觉,ES2018 <a href="https://github.com/tc39/proposal-regexp-dotall-flag" target="_blank" rel="noopener noreferrer">引入<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><code>s</code>修饰符,使得<code>.</code>可以匹配任意单个字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo.bar</span><span class="token regex-delimiter">/</span><span class="token regex-flags">s</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这被称为<code>dotAll</code>模式,即点(dot)代表一切字符。所以,正则表达式还引入了一个<code>dotAll</code>属性,返回一个布尔值,表示该正则表达式是否处在<code>dotAll</code>模式。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo.bar</span><span class="token regex-delimiter">/</span><span class="token regex-flags">s</span></span><span class="token punctuation">;</span>
- <span class="token comment">// 另一种写法</span>
- <span class="token comment">// const re = new RegExp('foo.bar', 's');</span>
- re<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'foo\nbar'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- re<span class="token punctuation">.</span>dotAll <span class="token comment">// true</span>
- re<span class="token punctuation">.</span>flags <span class="token comment">// 's'</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><code>/s</code>修饰符和多行修饰符<code>/m</code>不冲突,两者一起使用的情况下,<code>.</code>匹配所有字符,而<code>^</code>和<code>$</code>匹配每一行的行首和行尾。</p> <h2 id="后行断言"><a href="#后行断言" class="header-anchor">#</a> 后行断言</h2> <p>JavaScript 语言的正则表达式,只支持先行断言(lookahead)和先行否定断言(negative lookahead),不支持后行断言(lookbehind)和后行否定断言(negative lookbehind)。ES2018 引入<a href="https://github.com/tc39/proposal-regexp-lookbehind" target="_blank" rel="noopener noreferrer">后行断言<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>,V8 引擎 4.9 版(Chrome 62)已经支持。</p> <p>“先行断言”指的是,<code>x</code>只有在<code>y</code>前面才匹配,必须写成<code>/x(?=y)/</code>。比如,只匹配百分号之前的数字,要写成<code>/\d+(?=%)/</code>。“先行否定断言”指的是,<code>x</code>只有不在<code>y</code>前面才匹配,必须写成<code>/x(?!y)/</code>。比如,只匹配不在百分号之前的数字,要写成<code>/\d+(?!%)/</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d+(?=%)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'100% of US presidents have been male'</span><span class="token punctuation">)</span> <span class="token comment">// ["100"]</span>
- <span class="token operator">/</span>\d<span class="token operator">+</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator">!</span><span class="token operator">%</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'that’s all 44 of them'</span><span class="token punctuation">)</span> <span class="token comment">// ["44"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面两个字符串,如果互换正则表达式,就不会得到相同结果。另外,还可以看到,“先行断言”括号之中的部分(<code>(?=%)</code>),是不计入返回结果的。</p> <p>“后行断言”正好与“先行断言”相反,<code>x</code>只有在<code>y</code>后面才匹配,必须写成<code>/(?<=y)x/</code>。比如,只匹配美元符号之后的数字,要写成<code>/(?<=\$)\d+/</code>。“后行否定断言”则与“先行否定断言”相反,<code>x</code>只有不在<code>y</code>后面才匹配,必须写成<code>/(?<!y)x/</code>。比如,只匹配不在美元符号后面的数字,要写成<code>/(?<!\$)\d+/</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?<=\$)\d+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'Benjamin Franklin is on the $100 bill'</span><span class="token punctuation">)</span> <span class="token comment">// ["100"]</span>
- <span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator"><</span><span class="token operator">!</span>\$<span class="token punctuation">)</span>\d<span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'it’s is worth about €90'</span><span class="token punctuation">)</span> <span class="token comment">// ["90"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的例子中,“后行断言”的括号之中的部分(<code>(?<=\$)</code>),也是不计入返回结果。</p> <p>下面的例子是使用后行断言进行字符串替换。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DOLLAR_PREFIX</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?<=\$)foo</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
- <span class="token string">'$foo %foo foo'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token constant">RE_DOLLAR_PREFIX</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token comment">// '$bar %foo foo'</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,只有在美元符号后面的<code>foo</code>才会被替换。</p> <p>“后行断言”的实现,需要先匹配<code>/(?<=y)x/</code>的<code>x</code>,然后再回到左边,匹配<code>y</code>的部分。这种“先右后左”的执行顺序,与所有其他正则操作相反,导致了一些不符合预期的行为。</p> <p>首先,后行断言的组匹配,与正常情况下结果是不一样的。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?<=(\d+)(\d+))$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1053'</span><span class="token punctuation">)</span> <span class="token comment">// ["", "1", "053"]</span>
- <span class="token operator">/</span><span class="token operator">^</span><span class="token punctuation">(</span>\d<span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">(</span>\d<span class="token operator">+</span><span class="token punctuation">)</span>$<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1053'</span><span class="token punctuation">)</span> <span class="token comment">// ["1053", "105", "3"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,需要捕捉两个组匹配。没有“后行断言”时,第一个括号是贪婪模式,第二个括号只能捕获一个字符,所以结果是<code>105</code>和<code>3</code>。而“后行断言”时,由于执行顺序是从右到左,第二个括号是贪婪模式,第一个括号只能捕获一个字符,所以结果是<code>1</code>和<code>053</code>。</p> <p>其次,“后行断言”的反斜杠引用,也与通常的顺序相反,必须放在对应的那个括号之前。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?<=(o)d\1)r</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'hodor'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>
- <span class="token operator">/</span><span class="token punctuation">(</span><span class="token operator">?</span><span class="token operator"><=</span>\<span class="token number">1</span><span class="token function">d</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">)</span>r<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'hodor'</span><span class="token punctuation">)</span> <span class="token comment">// ["r", "o"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,如果后行断言的反斜杠引用(<code>\1</code>)放在括号的后面,就不会得到匹配结果,必须放在前面才可以。因为后行断言是先从左到右扫描,发现匹配以后再回过头,从右到左完成反斜杠引用。</p> <h2 id="unicode-属性类"><a href="#unicode-属性类" class="header-anchor">#</a> Unicode 属性类</h2> <p>ES2018 <a href="https://github.com/tc39/proposal-regexp-unicode-property-escapes" target="_blank" rel="noopener noreferrer">引入<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>了一种新的类的写法<code>\p{...}</code>和<code>\P{...}</code>,允许正则表达式匹配符合 Unicode 某种属性的所有字符。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> regexGreekSymbol <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\p{Script=Greek}</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
- regexGreekSymbol<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'π'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>\p{Script=Greek}</code>指定匹配一个希腊文字母,所以匹配<code>π</code>成功。</p> <p>Unicode 属性类要指定属性名和属性值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>\p<span class="token punctuation">{</span>UnicodePropertyName<span class="token operator">=</span>UnicodePropertyValue<span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>对于某些属性,可以只写属性名,或者只写属性值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>\p<span class="token punctuation">{</span>UnicodePropertyName<span class="token punctuation">}</span>
- \p<span class="token punctuation">{</span>UnicodePropertyValue<span class="token punctuation">}</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>\P{…}</code>是<code>\p{…}</code>的反向匹配,即匹配不满足条件的字符。</p> <p>注意,这两种类只对 Unicode 有效,所以使用的时候一定要加上<code>u</code>修饰符。如果不加<code>u</code>修饰符,正则表达式使用<code>\p</code>和<code>\P</code>会报错,ECMAScript 预留了这两个类。</p> <p>由于 Unicode 的各种属性非常多,所以这种新的类的表达能力非常强。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\p{Decimal_Number}+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
- regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'𝟏𝟐𝟑𝟜𝟝𝟞𝟩𝟪𝟫𝟬𝟭𝟮𝟯𝟺𝟻𝟼'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,属性类指定匹配所有十进制字符,可以看到各种字型的十进制字符都会匹配成功。</p> <p><code>\p{Number}</code>甚至能匹配罗马数字。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 匹配所有数字</span>
- <span class="token keyword">const</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\p{Number}+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
- regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'²³¹¼½¾'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'㉛㉜㉝'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>下面是其他一些例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 匹配所有空格</span>
- \p<span class="token punctuation">{</span>White_Space<span class="token punctuation">}</span>
- <span class="token comment">// 匹配各种文字的所有字母,等同于 Unicode 版的 \w</span>
- <span class="token punctuation">[</span>\p<span class="token punctuation">{</span>Alphabetic<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Mark<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Decimal_Number<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Connector_Punctuation<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Join_Control<span class="token punctuation">}</span><span class="token punctuation">]</span>
- <span class="token comment">// 匹配各种文字的所有非字母的字符,等同于 Unicode 版的 \W</span>
- <span class="token punctuation">[</span><span class="token operator">^</span>\p<span class="token punctuation">{</span>Alphabetic<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Mark<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Decimal_Number<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Connector_Punctuation<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Join_Control<span class="token punctuation">}</span><span class="token punctuation">]</span>
- <span class="token comment">// 匹配 Emoji</span>
- <span class="token operator">/</span>\p<span class="token punctuation">{</span>Emoji_Modifier_Base<span class="token punctuation">}</span>\p<span class="token punctuation">{</span>Emoji_Modifier<span class="token punctuation">}</span><span class="token operator">?</span><span class="token operator">|</span>\p<span class="token punctuation">{</span>Emoji_Presentation<span class="token punctuation">}</span><span class="token operator">|</span>\p<span class="token punctuation">{</span>Emoji<span class="token punctuation">}</span>\uFE0F<span class="token operator">/</span>gu
- <span class="token comment">// 匹配所有的箭头字符</span>
- <span class="token keyword">const</span> regexArrows <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\p{Block=Arrows}+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
- regexArrows<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'←↑→↓↔↕↖↗↘↙⇏⇐⇑⇒⇓⇔⇕⇖⇗⇘⇙⇧⇩'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="具名组匹配"><a href="#具名组匹配" class="header-anchor">#</a> 具名组匹配</h2> <h3 id="简介"><a href="#简介" class="header-anchor">#</a> 简介</h3> <p>正则表达式使用圆括号进行组匹配。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DATE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\d{4})-(\d{2})-(\d{2})</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,正则表达式里面有三组圆括号。使用<code>exec</code>方法,就可以将这三组匹配结果提取出来。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DATE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\d{4})-(\d{2})-(\d{2})</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> matchObj <span class="token operator">=</span> <span class="token constant">RE_DATE</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1999-12-31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> year <span class="token operator">=</span> matchObj<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 1999</span>
- <span class="token keyword">const</span> month <span class="token operator">=</span> matchObj<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 12</span>
- <span class="token keyword">const</span> day <span class="token operator">=</span> matchObj<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 31</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>组匹配的一个问题是,每一组的匹配含义不容易看出来,而且只能用数字序号(比如<code>matchObj[1]</code>)引用,要是组的顺序变了,引用的时候就必须修改序号。</p> <p>ES2018 引入了<a href="https://github.com/tc39/proposal-regexp-named-groups" target="_blank" rel="noopener noreferrer">具名组匹配<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>(Named Capture Groups),允许为每一个组匹配指定一个名字,既便于阅读代码,又便于引用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_DATE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> matchObj <span class="token operator">=</span> <span class="token constant">RE_DATE</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'1999-12-31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> year <span class="token operator">=</span> matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>year<span class="token punctuation">;</span> <span class="token comment">// 1999</span>
- <span class="token keyword">const</span> month <span class="token operator">=</span> matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>month<span class="token punctuation">;</span> <span class="token comment">// 12</span>
- <span class="token keyword">const</span> day <span class="token operator">=</span> matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>day<span class="token punctuation">;</span> <span class="token comment">// 31</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,“具名组匹配”在圆括号内部,模式的头部添加“问号 + 尖括号 + 组名”(<code>?<year></code>),然后就可以在<code>exec</code>方法返回结果的<code>groups</code>属性上引用该组名。同时,数字序号(<code>matchObj[1]</code>)依然有效。</p> <p>具名组匹配等于为每一组匹配加上了 ID,便于描述匹配的目的。如果组的顺序变了,也不用改变匹配后的处理代码。</p> <p>如果具名组没有匹配,那么对应的<code>groups</code>对象属性会是<code>undefined</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_OPT_A</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?<as>a+)?$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token keyword">const</span> matchObj <span class="token operator">=</span> <span class="token constant">RE_OPT_A</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- matchObj<span class="token punctuation">.</span>groups<span class="token punctuation">.</span>as <span class="token comment">// undefined</span>
- <span class="token string">'as'</span> <span class="token keyword">in</span> matchObj<span class="token punctuation">.</span>groups <span class="token comment">// true</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,具名组<code>as</code>没有找到匹配,那么<code>matchObj.groups.as</code>属性值就是<code>undefined</code>,并且<code>as</code>这个键名在<code>groups</code>是始终存在的。</p> <h3 id="解构赋值和替换"><a href="#解构赋值和替换" class="header-anchor">#</a> 解构赋值和替换</h3> <p>有了具名组匹配以后,可以使用解构赋值直接从匹配结果上为变量赋值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token punctuation">{</span><span class="token literal-property property">groups</span><span class="token operator">:</span> <span class="token punctuation">{</span>one<span class="token punctuation">,</span> two<span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?<one>.*):(?<two>.*)$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'foo:bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- one <span class="token comment">// foo</span>
- two <span class="token comment">// bar</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>字符串替换时,使用<code>$<组名></code>引用具名组。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> re <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})</span><span class="token regex-delimiter">/</span><span class="token regex-flags">u</span></span><span class="token punctuation">;</span>
- <span class="token string">'2015-01-02'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token string">'$<day>/$<month>/$<year>'</span><span class="token punctuation">)</span>
- <span class="token comment">// '02/01/2015'</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>replace</code>方法的第二个参数是一个字符串,而不是正则表达式。</p> <p><code>replace</code>方法的第二个参数也可以是函数,该函数的参数序列如下。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token string">'2015-01-02'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> <span class="token punctuation">(</span>
- matched<span class="token punctuation">,</span> <span class="token comment">// 整个匹配结果 2015-01-02</span>
- capture1<span class="token punctuation">,</span> <span class="token comment">// 第一个组匹配 2015</span>
- capture2<span class="token punctuation">,</span> <span class="token comment">// 第二个组匹配 01</span>
- capture3<span class="token punctuation">,</span> <span class="token comment">// 第三个组匹配 02</span>
- position<span class="token punctuation">,</span> <span class="token comment">// 匹配开始的位置 0</span>
- <span class="token constant">S</span><span class="token punctuation">,</span> <span class="token comment">// 原字符串 2015-01-02</span>
- groups <span class="token comment">// 具名组构成的一个对象 {year, month, day}</span>
- <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
- <span class="token keyword">let</span> <span class="token punctuation">{</span>day<span class="token punctuation">,</span> month<span class="token punctuation">,</span> year<span class="token punctuation">}</span> <span class="token operator">=</span> groups<span class="token punctuation">;</span>
- <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>day<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>month<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">/</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>year<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>具名组匹配在原来的基础上,新增了最后一个函数参数:具名组构成的一个对象。函数内部可以直接对这个对象进行解构赋值。</p> <h3 id="引用"><a href="#引用" class="header-anchor">#</a> 引用</h3> <p>如果要在正则表达式内部引用某个“具名组匹配”,可以使用<code>\k<组名></code>的写法。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_TWICE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?<word>[a-z]+)!\k<word>$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- <span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!ab'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>数字引用(<code>\1</code>)依然有效。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_TWICE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?<word>[a-z]+)!\1$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- <span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!ab'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这两种引用语法还可以同时使用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> <span class="token constant">RE_TWICE</span> <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^(?<word>[a-z]+)!\k<word>!\1$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
- <span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc!abc'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
- <span class="token constant">RE_TWICE</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc!abc!ab'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="string-prototype-matchall"><a href="#string-prototype-matchall" class="header-anchor">#</a> String.prototype.matchAll()</h2> <p>如果一个正则表达式在字符串里面有多个匹配,现在一般使用<code>g</code>修饰符或<code>y</code>修饰符,在循环里面逐一取出。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">t(e)(st(\d?))</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
- <span class="token keyword">var</span> string <span class="token operator">=</span> <span class="token string">'test1test2test3'</span><span class="token punctuation">;</span>
- <span class="token keyword">var</span> matches <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
- <span class="token keyword">var</span> match<span class="token punctuation">;</span>
- <span class="token keyword">while</span> <span class="token punctuation">(</span>match <span class="token operator">=</span> regex<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- matches<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- matches
- <span class="token comment">// [</span>
- <span class="token comment">// ["test1", "e", "st1", "1", index: 0, input: "test1test2test3"],</span>
- <span class="token comment">// ["test2", "e", "st2", "2", index: 5, input: "test1test2test3"],</span>
- <span class="token comment">// ["test3", "e", "st3", "3", index: 10, input: "test1test2test3"]</span>
- <span class="token comment">// ]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码中,<code>while</code>循环取出每一轮的正则匹配,一共三轮。</p> <p><a href="https://github.com/tc39/proposal-string-matchall" target="_blank" rel="noopener noreferrer">ES2020<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 增加了<code>String.prototype.matchAll()</code>方法,可以一次性取出所有匹配。不过,它返回的是一个遍历器(Iterator),而不是数组。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> string <span class="token operator">=</span> <span class="token string">'test1test2test3'</span><span class="token punctuation">;</span>
- <span class="token comment">// g 修饰符加不加都可以</span>
- <span class="token keyword">const</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">t(e)(st(\d?))</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
- <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> match <span class="token keyword">of</span> string<span class="token punctuation">.</span><span class="token function">matchAll</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>match<span class="token punctuation">)</span><span class="token punctuation">;</span>
- <span class="token punctuation">}</span>
- <span class="token comment">// ["test1", "e", "st1", "1", index: 0, input: "test1test2test3"]</span>
- <span class="token comment">// ["test2", "e", "st2", "2", index: 5, input: "test1test2test3"]</span>
- <span class="token comment">// ["test3", "e", "st3", "3", index: 10, input: "test1test2test3"]</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,由于<code>string.matchAll(regex)</code>返回的是遍历器,所以可以用<code>for...of</code>循环取出。相对于返回数组,返回遍历器的好处在于,如果匹配结果是一个很大的数组,那么遍历器比较节省资源。</p> <p>遍历器转为数组是非常简单的,使用<code>...</code>运算符和<code>Array.from()</code>方法就可以了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 转为数组方法一</span>
- <span class="token punctuation">[</span><span class="token operator">...</span>string<span class="token punctuation">.</span><span class="token function">matchAll</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">]</span>
- <span class="token comment">// 转为数组方法二</span>
- Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>string<span class="token punctuation">.</span><span class="token function">matchAll</span><span class="token punctuation">(</span>regex<span class="token punctuation">)</span><span class="token punctuation">)</span>
- </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/heBody/blog/edit/master/docs/《ES6 教程》笔记/06.正则的扩展.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=ES6" title="标签">#ES6</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/12/14, 19:36:42</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/pages/a650b4a0ebfc9350/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">字符串的新增方法</div></a> <a href="/blog/pages/5dfea9a0f2d1a392/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">数值的扩展</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
- ←
- <a href="/blog/pages/a650b4a0ebfc9350/" class="prev">字符串的新增方法</a></span> <span class="next"><a href="/blog/pages/5dfea9a0f2d1a392/">数值的扩展</a>→
- </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/922650/"><div>
- Git修改分支名
- <!----></div></a> <span class="date">08-11</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/55f894/"><div>
- CSS给table的tbody添加滚动条
- <!----></div></a> <span class="date">06-29</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/829589/"><div>
- 我做了一个手写春联小网页,祝大家虎年暴富
- <span class="title-tag">
- 原创
- </span></div></a> <span class="date">01-28</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章></a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:30363811@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/heBody" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
- Copyright © 2016-2022
- <span>Hesb | <a href="https://github.com/heBody/blob" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
- 跟随系统
- </li><li class="iconfont icon-rijianmoshi">
- 浅色模式
- </li><li class="iconfont icon-yejianmoshi">
- 深色模式
- </li><li class="iconfont icon-yuedu">
- 阅读模式
- </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
- <script src="/blog/assets/js/app.14d95a24.js" defer></script><script src="/blog/assets/js/2.395c0d18.js" defer></script><script src="/blog/assets/js/3.6748bd5c.js" defer></script><script src="/blog/assets/js/93.c8ee75e3.js" defer></script>
- </body>
- </html>
|