{"id":490,"date":"2025-12-05T10:41:02","date_gmt":"2025-12-05T02:41:02","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=490"},"modified":"2025-12-05T10:41:02","modified_gmt":"2025-12-05T02:41:02","slug":"typescript%e4%b8%ad%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e7%94%a8%e6%b3%95%e5%8f%8a%e5%ae%9e%e9%99%85%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/typescript%e4%b8%ad%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e7%94%a8%e6%b3%95%e5%8f%8a%e5%ae%9e%e9%99%85%e5%ba%94%e7%94%a8\/","title":{"rendered":"TypeScript\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u53ca\u5b9e\u9645\u5e94\u7528"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>TypeScript \u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u53ca\u5b9e\u9645\u5e94\u7528\u8be6\u89e3<\/strong>\uff0c\u4ece\u57fa\u7840\u8bed\u6cd5\u3001TS \u7c7b\u578b\u7ea6\u675f\uff0c\u5230\u5b9e\u9645\u9879\u76ee\u5e94\u7528\u573a\u666f\uff0c\u5168\u65b9\u4f4d\u8986\u76d6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 \u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840\u8bed\u6cd5<\/h1>\n\n\n\n<p>\u5728 TypeScript \u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u4e3a <code>RegExp<\/code>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst reg: RegExp = \/abc\/          \/\/ \u5b57\u9762\u91cf\nconst reg2: RegExp = new RegExp(&quot;abc&quot;) \/\/ \u6784\u9020\u51fd\u6570\n\n<\/pre><\/div>\n\n\n<p>\u5e38\u7528\u6b63\u5219\u4fee\u9970\u7b26\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4fee\u9970\u7b26<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td><code>g<\/code><\/td><td>\u5168\u5c40\u5339\u914d<\/td><\/tr><tr><td><code>i<\/code><\/td><td>\u5ffd\u7565\u5927\u5c0f\u5199<\/td><\/tr><tr><td><code>m<\/code><\/td><td>\u591a\u884c\u5339\u914d<\/td><\/tr><tr><td><code>s<\/code><\/td><td>\u70b9\u53f7\u5339\u914d\u6362\u884c\u7b26<\/td><\/tr><tr><td><code>u<\/code><\/td><td>Unicode \u6a21\u5f0f<\/td><\/tr><tr><td><code>y<\/code><\/td><td>\u7c98\u8fde\u5339\u914d<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 RegExp \u5e38\u7528\u65b9\u6cd5<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 <code>test()<\/code> \u5224\u65ad\u662f\u5426\u5339\u914d<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst regex: RegExp = \/^&#x5B;a-z]+$\/i\nconsole.log(regex.test(&quot;Hello&quot;))  \/\/ true\nconsole.log(regex.test(&quot;123&quot;))    \/\/ false\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2.2 <code>exec()<\/code> \u83b7\u53d6\u5339\u914d\u7ed3\u679c<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst regex = \/(\\d{4})-(\\d{2})-(\\d{2})\/\nconst result = regex.exec(&quot;2025-12-05&quot;)\nconsole.log(result)\n\/\/ &#x5B;\n\/\/   &quot;2025-12-05&quot;, &quot;2025&quot;, &quot;12&quot;, &quot;05&quot;\n\/\/ ]\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2.3 \u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst str = &quot;foo123bar456&quot;\n\n\/\/ match\nconsole.log(str.match(\/\\d+\/g)) \/\/ &#x5B;&quot;123&quot;, &quot;456&quot;]\n\n\/\/ replace\nconsole.log(str.replace(\/\\d+\/g, &quot;#&quot;)) \/\/ foo#bar#\n\n\/\/ split\nconsole.log(str.split(\/\\d+\/)) \/\/ &#x5B;&quot;foo&quot;, &quot;bar&quot;, &quot;&quot;]\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3\ufe0f\u20e3 \u6b63\u5219\u8868\u8fbe\u5f0f\u5e38\u89c1\u6a21\u5f0f<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6a21\u5f0f<\/th><th>\u793a\u4f8b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>\\d<\/code><\/td><td><code>\/\\d+\/<\/code><\/td><td>\u5339\u914d\u6570\u5b57<\/td><\/tr><tr><td><code>\\D<\/code><\/td><td><code>\/\\D+\/<\/code><\/td><td>\u5339\u914d\u975e\u6570\u5b57<\/td><\/tr><tr><td><code>\\w<\/code><\/td><td><code>\/\\w+\/<\/code><\/td><td>\u5339\u914d\u5b57\u6bcd\u6570\u5b57\u4e0b\u5212\u7ebf<\/td><\/tr><tr><td><code>\\W<\/code><\/td><td><code>\/\\W+\/<\/code><\/td><td>\u5339\u914d\u975e\u5b57\u6bcd\u6570\u5b57\u4e0b\u5212\u7ebf<\/td><\/tr><tr><td><code>\\s<\/code><\/td><td><code>\/\\s+\/<\/code><\/td><td>\u5339\u914d\u7a7a\u767d<\/td><\/tr><tr><td><code>\\S<\/code><\/td><td><code>\/\\S+\/<\/code><\/td><td>\u5339\u914d\u975e\u7a7a\u767d<\/td><\/tr><tr><td><code>.<\/code><\/td><td><code>\/a.b\/<\/code><\/td><td>\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/td><\/tr><tr><td><code>^<\/code><\/td><td><code>\/^abc\/<\/code><\/td><td>\u5339\u914d\u5f00\u5934<\/td><\/tr><tr><td><code>$<\/code><\/td><td><code>\/abc$\/<\/code><\/td><td>\u5339\u914d\u7ed3\u5c3e<\/td><\/tr><tr><td><code>[]<\/code><\/td><td><code>\/[a-z]\/<\/code><\/td><td>\u5b57\u7b26\u96c6\u5408<\/td><\/tr><tr><td>`<\/td><td>`<\/td><td>`\/foo<\/td><\/tr><tr><td><code>()<\/code><\/td><td><code>\/(\\d+)\/<\/code><\/td><td>\u5206\u7ec4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4\ufe0f\u20e3 TypeScript \u4e2d\u7684\u7c7b\u578b\u7ea6\u675f<\/h1>\n\n\n\n<p>TS \u53ef\u4ee5\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\uff0c\u786e\u4fdd\u4f20\u5165\u51fd\u6570\u7684\u662f <code>RegExp<\/code> \u7c7b\u578b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction validateInput(input: string, pattern: RegExp): boolean {\n  return pattern.test(input)\n}\n\nconst phonePattern: RegExp = \/^\\d{10,11}$\/\nconsole.log(validateInput(&quot;13812345678&quot;, phonePattern)) \/\/ true\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5\ufe0f\u20e3 \u5b9e\u9645\u5e94\u7528\u573a\u666f\u793a\u4f8b<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 \u624b\u673a\u53f7\u6821\u9a8c<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction isPhoneNumber(phone: string): boolean {\n  const pattern: RegExp = \/^1&#x5B;3-9]\\d{9}$\/\n  return pattern.test(phone)\n}\n\nconsole.log(isPhoneNumber(&quot;13812345678&quot;)) \/\/ true\nconsole.log(isPhoneNumber(&quot;123456&quot;))      \/\/ false\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5.2 \u90ae\u7bb1\u6821\u9a8c<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction isEmail(email: string): boolean {\n  const pattern: RegExp = \/^&#x5B;\\w.-]+@&#x5B;\\w.-]+\\.&#x5B;a-zA-Z]{2,6}$\/\n  return pattern.test(email)\n}\n\nconsole.log(isEmail(&quot;test@example.com&quot;)) \/\/ true\nconsole.log(isEmail(&quot;test@.com&quot;))        \/\/ false\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5.3 \u65e5\u671f\u683c\u5f0f\u6821\u9a8c (YYYY-MM-DD)<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction isDate(date: string): boolean {\n  const pattern: RegExp = \/^\\d{4}-\\d{2}-\\d{2}$\/\n  return pattern.test(date)\n}\n\nconsole.log(isDate(&quot;2025-12-05&quot;)) \/\/ true\nconsole.log(isDate(&quot;2025\/12\/05&quot;)) \/\/ false\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5.4 \u5bc6\u7801\u5f3a\u5ea6\u6821\u9a8c<\/h3>\n\n\n\n<p>\u8981\u6c42\uff1a8-16 \u4f4d\uff0c\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction isStrongPassword(pwd: string): boolean {\n  const pattern: RegExp = \/^(?=.*&#x5B;A-Za-z])(?=.*\\d)&#x5B;A-Za-z\\d]{8,16}$\/\n  return pattern.test(pwd)\n}\n\nconsole.log(isStrongPassword(&quot;abc12345&quot;)) \/\/ true\nconsole.log(isStrongPassword(&quot;12345678&quot;)) \/\/ false\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5.5 \u63d0\u53d6\u6587\u672c\u4e2d\u7684\u6570\u5b57<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst text = &quot;\u8ba2\u5355\u53f7: 20251205, \u91d1\u989d: 1000&quot;\nconst numbers = text.match(\/\\d+\/g)\nconsole.log(numbers) \/\/ &#x5B;&quot;20251205&quot;, &quot;1000&quot;]\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">6\ufe0f\u20e3 \u7ec4\u5408\u5f0f\u5e94\u7528\uff08\u51fd\u6570\u5c01\u88c5\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction validate(pattern: RegExp, value: string): boolean {\n  return pattern.test(value)\n}\n\n\/\/ \u624b\u673a\u53f7\nconsole.log(validate(\/^1&#x5B;3-9]\\d{9}$\/, &quot;13812345678&quot;)) \/\/ true\n\n\/\/ \u90ae\u7bb1\nconsole.log(validate(\/^&#x5B;\\w.-]+@&#x5B;\\w.-]+\\.&#x5B;a-zA-Z]{2,6}$\/, &quot;test@example.com&quot;)) \/\/ true\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">7\ufe0f\u20e3 \u603b\u7ed3<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>TypeScript \u7684\u4f18\u52bf<\/strong>\uff1a\u5f3a\u7c7b\u578b\u7ea6\u675f\uff0c\u786e\u4fdd\u4f20\u5165 <code>RegExp<\/code> \u6216\u5b57\u7b26\u4e32\u6b63\u786e\u3002<\/li>\n\n\n\n<li><strong>\u5e38\u89c1\u6b63\u5219\u4f7f\u7528<\/strong>\uff1a\u6821\u9a8c\u624b\u673a\u53f7\u3001\u90ae\u7bb1\u3001\u5bc6\u7801\u3001\u65e5\u671f\u3001\u63d0\u53d6\u6570\u5b57\u3001\u5206\u5272\u5b57\u7b26\u4e32\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u6218\u6280\u5de7<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u6b63\u5219\u8fdb\u884c\u5c01\u88c5\u51fd\u6570\uff0c\u63d0\u9ad8\u590d\u7528\u6027<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>match<\/code> \/ <code>replace<\/code> \/ <code>split<\/code> \u505a\u6587\u672c\u5904\u7406<\/li>\n\n\n\n<li>\u5bf9\u52a8\u6001\u6b63\u5219\u4f7f\u7528 <code>new RegExp()<\/code> \u6784\u9020<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd TypeScript \u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5\u53ca\u5b9e\u9645\u5e94\u7528\u8be6\u89e3\uff0c\u4ece\u57fa\u7840&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/typescript%e4%b8%ad%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e7%94%a8%e6%b3%95%e5%8f%8a%e5%ae%9e%e9%99%85%e5%ba%94%e7%94%a8\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-490","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions"}],"predecessor-version":[{"id":491,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions\/491"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}