{"id":4948,"date":"2023-01-05T11:24:41","date_gmt":"2023-01-05T03:24:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4948"},"modified":"2023-01-05T11:24:41","modified_gmt":"2023-01-05T03:24:41","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e5%ad%97%e4%b8%b2%e7%9a%84%e6%b1%82%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e5%ad%97%e4%b8%b2%e7%9a%84%e6%b1%82%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f%e5%8c%b9%e9%85%8d\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u7684\u6c42\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u5b57\u4e32, \u6b63\u5247\u8868\u9054\u5f0f, \u5339\u914d, String Regular Expression Matching\"><\/p>\n<h1>TypeScript \u5b57\u4e32\u7684\u6c42\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u958b\u767c\u51fa\u66f4\u512a\u79c0\u7684\u7a0b\u5f0f\u78bc\u3002\u5176\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u7279\u6027\u5c31\u662f\u652f\u63f4\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u641c\u5c0b\u548c\u5339\u914d\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <span style=\"font-weight: bold; color: #0099cc;\">RegExp<\/span> \u985e\u5225\u4f86\u5efa\u7acb\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u4e26\u4f7f\u7528 <span style=\"font-weight: bold; color: #0099cc;\">test()<\/span> \u65b9\u6cd5\u4f86\u6e2c\u8a66\u5b57\u4e32\u662f\u5426\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f\u7684\u898f\u5247\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6703\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f <code>\/^[A-Z]\/<\/code>\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet regex = new RegExp('\/^[A-Z]\/');\nlet str = 'Hello World';\n\nif (regex.test(str)) {\n  console.log('\u5b57\u4e32\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f');\n} else {\n  console.log('\u5b57\u4e32\u4e0d\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f');\n}\n<\/pre>\n<p>\u6b64\u5916\uff0cTypeScript \u4e5f\u652f\u63f4 <span style=\"font-weight: bold; color: #0099cc;\">match()<\/span> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u641c\u5c0b\u5b57\u4e32\u4e2d\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f\u7684\u90e8\u5206\uff0c\u4e26\u5c07\u7d50\u679c\u4ee5\u9663\u5217\u7684\u5f62\u5f0f\u56de\u50b3\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6703\u641c\u5c0b\u5b57\u4e32\u4e2d\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f <code>\/[A-Z]\/<\/code> \u7684\u90e8\u5206\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet regex = new RegExp('\/[A-Z]\/');\nlet str = 'Hello World';\n\nlet matches = str.match(regex);\n\nconsole.log(matches); \/\/ ['H', 'W']\n<\/pre>\n<p>\u6b64\u5916\uff0cTypeScript \u4e5f\u652f\u63f4 <span style=\"font-weight: bold; color: #0099cc;\">replace()<\/span> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u5c07\u5b57\u4e32\u4e2d\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f\u7684\u90e8\u5206\u66ff\u63db\u6210\u5176\u4ed6\u5b57\u4e32\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6703\u5c07\u5b57\u4e32\u4e2d\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f <code>\/[A-Z]\/<\/code> \u7684\u90e8\u5206\u66ff\u63db\u6210 <code>#<\/code>\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet regex = new RegExp('\/[A-Z]\/');\nlet str = 'Hello World';\n\nlet replaced = str.replace(regex, '#');\n\nconsole.log(replaced); \/\/ '#ello #orld'\n<\/pre>\n<p>\u900f\u904e TypeScript \u652f\u63f4\u7684\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u641c\u5c0b\u548c\u5339\u914d\uff0c\u4e26\u5c0d\u5b57\u4e32\u9032\u884c\u66ff\u63db\uff0c\u4ee5\u6eff\u8db3\u61c9\u7528\u7a0b\u5f0f\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aTypeScript\u5b57\u4e32\u7684\u6c42\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\u662f\u4e00\u7a2e\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u5feb\u5730\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u7b26\u5408\u67d0\u7a2e\u6a21\u5f0f\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u63a7\u5236\u548c\u7cbe\u78ba\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u5b57\u4e32\u7684\u6c42\u6b63\u5247\u8868\u9054\u5f0f\u5339\u914d\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-4948","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1hO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4948"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4948\/revisions"}],"predecessor-version":[{"id":4949,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4948\/revisions\/4949"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}