text: "use strict";(self.webpackChunkvuepress_blog=self.webpackChunkvuepress_blog||[]).push([[85309],{51306:(n,s,a)=>{a.r(s),a.d(s,{data:()=>p});const p={key:"v-7164d7e7",path:"/typora/01frontEnd/02JavaScript/053%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.html",title:"53 正则表达式",lang:"zh-CN",frontmatter:{},excerpt:"",headers:[{level:2,title:"01 背景介绍",slug:"_01-背景介绍",children:[]},{level:2,title:"02 基础介绍",slug:"_02-基础介绍",children:[]},{level:2,title:"03 常用正则表达式",slug:"_03-常用正则表达式",children:[]},{level:2,title:"04 正则表达式格式化日期",slug:"_04-正则表达式格式化日期",children:[]}],filePathRelative:"typora/01frontEnd/02JavaScript/053正则表达式.md",git:{updatedTime:1631527134e3}}},41592:(n,s,a)=>{a.r(s),a.d(s,{default:()=>t});const p=(0,a(36425).uE)('<h1 id="_53-正则表达式" tabindex="-1"><a class="header-anchor" href="#_53-正则表达式" aria-hidden="true">#</a> 53 正则表达式</h1><h2 id="_01-背景介绍" tabindex="-1"><a class="header-anchor" href="#_01-背景介绍" aria-hidden="true">#</a> 01 背景介绍</h2><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">/*\n1.什么是正则表达式?\n正则表达式就是对字符串操作的一种逻辑公式\n\n2.正则表达式的作用?\n1.在字符串&quot;查找&quot;是否包含指定子串\n2.从字符串中&quot;提取&quot;指定子串\n3.对字符串中指定的内容进行&quot;替换&quot;\n */</span>\n<span class="token comment">// 1.字符串查找</span>\n<span class="token comment">/*\nlet str = &quot;123abc456&quot;;\nlet index = str.indexOf(&quot;abc&quot;);\nlet index = str.lastIndexOf(&quot;abc&quot;);\nlet flag = str.includes(&quot;abc&quot;);\n */</span>\n<span class="token comment">// 2.字符串提取</span>\n<span class="token comment">/*\nlet str = &quot;123abc456&quot;;\nlet startIndex = str.indexOf(&quot;a&quot;);\nconsole.log(str.substr(startIndex, &quot;abc&quot;.length));\n */</span>\n<span class="token comment">// 3.字符串替换</span>\n<span class="token comment">/*\nlet str = &quot;123abc456&quot;;\nstr.replace(&quot;abc&quot;, &quot;it666&quot;);\n */</span>\n\n</code></pre><div class="line-numbers"><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><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h2 id="_02-基础介绍" tabindex="-1"><a class="header-anchor" href="#_02-基础介绍" aria-hidden="true">#</a> 02 基础介绍</h2><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code>\n<span class="token comment">// 1.利用正则表达式匹配(查找)</span>\n<span class="token comment">/*\nlet str = &quot;123abc456&quot;;\n// 1.创建一个正则表达式对象\n// 2.指定匹配的规则\n// 注意点: 默认情况下在正则表达式中是区分大小写的\nlet reg = new RegExp(&quot;A&quot;, &quot;i&quot;);\nlet res = reg.test(str);\nconsole.log(res);\n */</span>\n<span class="token comment">/*\nlet str = &quot;abc2020-1-11def&quot;;\n// 通过构造函数创建正则表达式对象\n// let reg = new RegExp(&quot;\\\\d{4}-\\\\d{1,2}-\\\\d{1,2}&quot;);\n// 通过字面量来创建正则表达式对象\nlet reg = /\\d{4}-\\d{1,2}-\\d{1,2}/;\nlet res = reg.test(str);\nconsole.log(res);\n */</span>\n\n<span class="token comment">// 2.通过正则表达式提取符合规则的字符串</span>\n<span class="token comment">/*\nlet str = &quot;abc2020-1-11def2019-11-11fdjsklf&quot;;\n// 注意点: 默认情况下在正则表达式中一旦匹配就会停止查找\nlet reg = /\\d{4}-\\d{1,2}-\\d{1,2}/g;\nlet res = str.match(reg);\nconsole.log(res);\nconsole.log(res[0]);\nconsole.log(res[1]);\n */</span>\n\n<span class="token comment">// 3.通过正则表达式替换符合规则的字符串</span>\n<span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">&quot;abc2020-1-11def2019-11-11fdjsklf&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">let</span> reg <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{1,2}-\\d{1,2}</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>\n<span class="token keyword">let</span> newStr <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>reg<span class="token punctuation">,</span> <span class="token string">&quot;it666&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\nconsole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>\nconsole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newStr<span class="token punctuation">)</span><span class="token punctuation">;</span>\n</code></pre><div class="line-numbers"><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><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><p><strong>总结：</strong></p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 1. test方法</span>\n<span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">&#39;abc123456adfj&#39;</span>\n<span class="token keyword">let</span> reg <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>\nreg<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token comment">// true 判断字符串是否包含a </span>\n\n<span class="token comment">// 2. i不区分大小写</span>\n<span class="token keyword">let</span> reg <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">i</span></span>\n\n<span class="token comment">// 3. g全局，从上到下匹配所有的，而非匹配到第一个就停止</span>\n\n<span class="token comment">// 4. 元字符，例如\\d匹配任意一个数字，</span>\n<span class="token comment">// 4.1 但使用正则对象创建正则表达式时要写为\\\\d</span>\n<span class="token comment">// 4.2 使用字面量创建正则表达式时写为\\d，例如/\\d/</span>\n\n<span class="token comment">// 5. 量词，</span>\n<span class="token comment">// 5.1   例如\\d{4}，表示查找像2010这样的四个数字</span>\n<span class="token comment">// 5.2   例如\\d{1,2}，表示查找一个05或5的数字，最少查找1个，最多查找2个</span>\n</code></pre><div class="line-numbers"><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><span class="line-number">17</span><br></div></div><h2 id="_03-常用正则表达式" tabindex="-1"><a class="header-anchor" href="#_03-常用正则表达式" aria-hidden="true">#</a> 03 常用正则表达式</h2><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>&lt;!--\n常用正则表达式合集:\n验证数字：^[0-9]*$\n验证n位的数字：^\\d{n}$\n验证至少n位数字：^\\d{n,}$\n验证m-n位的数字：^\\d{m,n}$\n验证零和非零开头的数字：^(0|[1-9][0-9]*)$\n验证有两位小数的正实数：^[0-9]+(.[0-9]{2})?$\n验证有1-3位小数的正实数：^[0-9]+(.[0-9]{1,3})?$\n验证非零的正整数：^\\+?[1-9][0-9]*$\n验证非零的负整数：^\\-[1-9][0-9]*$\n验证非负整数（正整数 + 0）  ^\\d+$\n验证非正整数（负整数 + 0）  ^((-\\d+)|(0+))$\n验证长度为3的字符：^.{3}$\n验证由26个英文字母组成的字符串：^[A-Za-z]+$\n验证由26个大写英文字母组成的字符串：^[A-Z]+$\n验证由26个小写英文字母组成的字符串：^[a-z]+$\n验证由数字和26个英文字母组成的字符串：^[A-Za-z0-9]+$\n验证由数字、26个英文字母或者下划线组成的字符串：^\\w+$\n\n验证用户密码:^[a-zA-Z]\\w{5,17}$ 正确格式为：以字母开头，长度在6-18之间，只能包含字符、数字和下划线。\n验证是否含有 ^%&amp;&#39;,;=?$\\&quot; 等字符：[^%&amp;&#39;,;=?$\\x22]+\n验证汉字：^[\\u4e00-\\u9fa5],{0,}$\n验证Email地址：^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$\n验证InternetURL：^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&amp;=]*)?$ ；^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$\n验证电话号码：^(\\d3,4|\\d{3,4}-)?\\d{7,8}$：--正确格式为：XXXX-XXXXXXX，XXXX-XXXXXXXX，XXX-XXXXXXX，XXX-XXXXXXXX，XXXXXXX，XXXXXXXX。\n验证身份证号（15位或18位数字）：^\\d{15}|\\d{}18$\n验证一年的12个月：^(0?[1-9]|1[0-2])$ 正确格式为：“01”-“09”和“1”“12”\n验证一个月的31天：^((0?[1-9])|((1|2)[0-9])|30|31)$    正确格式为：01、09和1、31。\n整数：^-?\\d+$\n非负浮点数（正浮点数 + 0）：^\\d+(\\.\\d+)?$\n正浮点数   ^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$\n非正浮点数（负浮点数 + 0） ^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$\n负浮点数  ^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$\n浮点数  ^(-?\\d+)(\\.\\d+)?$\n--&gt;\n</code></pre><div class="line-numbers"><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><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><h2 id="_04-正则表达式格式化日期" tabindex="-1"><a class="header-anchor" href="#_04-正则表达式格式化日期" aria-hidden="true">#</a> 04 正则表达式格式化日期</h2><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> crtTime <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token comment">/*\ny  年\nM  月\nd  天\nh 小时\nm 分钟\ns 秒钟\n*/</span>\n<span class="token keyword">let</span> res1 <span class="token operator">=</span> <span class="token function">dateFormart</span><span class="token punctuation">(</span><span class="token string">&quot;yyyy-MM-dd hh:mm:ss&quot;</span><span class="token punctuation">,</span> crtTime<span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token keyword">let</span> res2 <span class="token operator">=</span> <span class="token function">dateFormart</span><span class="token punctuation">(</span><span class="token string">&quot;yyyy-MM-dd&quot;</span><span class="token punctuation">,</span> crtTime<span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token keyword">let</span> res3 <span class="token operator">=</span> <span class="token function">dateFormart</span><span class="token punctuation">(</span><span class="token string">&quot;hh:mm:ss&quot;</span><span class="token punctuation">,</span> crtTime<span class="token punctuation">)</span><span class="token punctuation">;</span>\nconsole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res1<span class="token punctuation">)</span><span class="token punctuation">;</span>\nconsole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res2<span class="token punctuation">)</span><span class="token punctuation">;</span>\nconsole<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res3<span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function">dateFormart</span><span class="token punctuation">(</span><span class="token parameter">fmt<span class="token punctuation">,</span> date</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n    <span class="token comment">// 1.处理年</span>\n    <span class="token comment">// 1.1找到yyyy</span>\n    <span class="token comment">// +在正则表达式中表示匹配1个或多个连续的指定字符</span>\n    <span class="token comment">// let reg = /y+/;</span>\n    <span class="token keyword">let</span> yearStr <span class="token operator">=</span> fmt<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">y+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token keyword">if</span><span class="token punctuation">(</span>yearStr<span class="token punctuation">)</span><span class="token punctuation">{</span>\n        yearStr <span class="token operator">=</span> yearStr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>\n        <span class="token comment">// 1.2获取到当前的年</span>\n        <span class="token keyword">let</span> yearNum <span class="token operator">=</span> date<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span> <span class="token comment">// 2019</span>\n        yearNum <span class="token operator">=</span> yearNum<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">4</span> <span class="token operator">-</span> yearStr<span class="token punctuation">.</span>length<span class="token punctuation">)</span>\n        <span class="token comment">// 1.3利用当前的年替换掉yyyy</span>\n        fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>yearStr<span class="token punctuation">,</span> yearNum<span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n    <span class="token comment">// 2.处理其他的时间</span>\n    <span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>\n        <span class="token string">&quot;M+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span>\n        <span class="token string">&quot;d+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>\n        <span class="token string">&quot;h+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>\n        <span class="token string">&quot;m+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>\n        <span class="token string">&quot;s+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span>\n    <span class="token punctuation">}</span><span class="token punctuation">;</span>\n    <span class="token comment">// 2.1遍历取出所有的时间</span>\n    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>\n        <span class="token comment">// let reg = new RegExp(&quot;M+&quot;);</span>\n        <span class="token keyword">let</span> reg <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 template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>key<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>\n        <span class="token comment">// 取出格式化字符串中对应的格式字符 MM dd hh mm ss</span>\n        <span class="token keyword">let</span> fmtStr <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>reg<span class="token punctuation">)</span><span class="token punctuation">;</span>\n        <span class="token keyword">if</span><span class="token punctuation">(</span>fmtStr<span class="token punctuation">)</span><span class="token punctuation">{</span>\n            fmtStr <span class="token operator">=</span> fmtStr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>\n            <span class="token comment">// 单独处理一位或者两位的时间</span>\n            <span class="token keyword">if</span><span class="token punctuation">(</span>fmtStr<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>\n                <span class="token comment">// 一位</span>\n                fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>fmtStr<span class="token punctuation">,</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n            <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>\n                <span class="token comment">// 两位</span>\n                <span class="token keyword">let</span> numStr <span class="token operator">=</span> <span class="token string">&quot;00&quot;</span> <span class="token operator">+</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span>\n                <span class="token comment">//&quot;00&quot; + 4 = &quot;004&quot;</span>\n                <span class="token comment">//&quot;00&quot; + 23 = &quot;0023&quot;</span>\n                numStr <span class="token operator">=</span> numStr<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>\n                fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>fmtStr<span class="token punctuation">,</span> numStr<span class="token punctuation">)</span><span class="token punctuation">;</span>\n            <span class="token punctuation">}</span>\n        <span class="token punctuation">}</span>\n    <span class="token punctuation">}</span>\n    <span class="token comment">// 3.将格式化之后的字符串返回</span>\n    <span class="token keyword">return</span> fmt<span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token doc-comment comment">/**************************************时间格式化处理************************************/</span>\n<span class="token keyword">function</span> <span class="token function">dateFmt</span><span class="token punctuation">(</span><span class="token parameter">fmt<span class="token punctuation">,</span>date</span><span class="token punctuation">)</span><span class="token punctuation">{</span>\n    <span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>\n        <span class="token string">&quot;M+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>                 <span class="token comment">//月份</span>\n        <span class="token string">&quot;d+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>                    <span class="token comment">//日</span>\n        <span class="token string">&quot;h+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>                   <span class="token comment">//小时</span>\n        <span class="token string">&quot;m+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>                 <span class="token comment">//分</span>\n        <span class="token string">&quot;s+&quot;</span> <span class="token operator">:</span> date<span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>                 <span class="token comment">//秒</span>\n    <span class="token punctuation">}</span><span class="token punctuation">;</span>\n    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(y+)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>fmt<span class="token punctuation">)</span><span class="token punctuation">)</span>\n        fmt<span class="token operator">=</span>fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>RegExp<span class="token punctuation">.</span>$<span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>date<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">4</span> <span class="token operator">-</span> RegExp<span class="token punctuation">.</span>$<span class="token number">1.</span>length<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span>\n        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>key<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 function">test</span><span class="token punctuation">(</span>fmt<span class="token punctuation">)</span><span class="token punctuation">)</span>\n            fmt <span class="token operator">=</span> fmt<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>RegExp<span class="token punctuation">.</span>$<span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>RegExp<span class="token punctuation">.</span>$<span class="token number">1.</span>length<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token punctuation">(</span>obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">&quot;00&quot;</span><span class="token operator">+</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token operator">+</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token keyword">return</span> fmt<span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n</code></pre><div class="line-numbers"><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><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br></div></div>',11),t={render:function(n,s){return p}}}}]);