{"id":3262,"date":"2024-11-10T10:43:55","date_gmt":"2024-11-10T02:43:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3262\/"},"modified":"2024-11-10T10:43:55","modified_gmt":"2024-11-10T02:43:55","slug":"js%e5%a6%82%e4%bd%95%e5%86%99%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3262\/","title":{"rendered":"js\u5982\u4f55\u5199\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 regexp \u5bf9\u8c61\u6765\u521b\u5efa\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\u5143\u5b57\u7b26\u548c\u6a21\u5f0f\u9650\u5b9a\u7b26\u6765\u5339\u914d\u6587\u672c\u3002\u5e38\u89c1\u5143\u5b57\u7b26\u5305\u62ec .\uff08\u5339\u914d\u9664\u6362\u884c\u7b26\u5916\u7684\u4efb\u4f55\u5b57\u7b26\uff09\u3001*\uff08\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u591a\u6b21\uff09\u3001+\uff08\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\uff09\u3001?\uff08\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u4e00\u6b21\uff09\u3001^\uff08\u5339\u914d\u5b57\u7b26\u4e32\u5f00\u5934\uff09\u3001$\uff08\u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u5c3e\uff09\u3001[]\uff08\u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\uff09\u3002\u6a21\u5f0f\u9650\u5b9a\u7b26\u5305\u62ec g\uff08\u5168\u5c40\u641c\u7d22\uff09\u3001i\uff08\u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff09\u3001m\uff08\u591a\u884c\uff09\u3001s\uff08\u70b9\u8bed\u6cd5\uff09\u548c u\uff08unicode\uff09\u3002\u53ef\u4ee5\u4f7f\u7528 test()\u3001exec()\u3001match()\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110808363445021.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f (regex) \u662f\u4e00\u79cd\u6a21\u5f0f\u5339\u914d\u8bed\u8a00\uff0c\u5b83\u7528\u4e8e\u67e5\u627e\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 RegExp \u5bf9\u8c61\u6765\u521b\u5efa\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>const regex = new RegExp(pattern, flags);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>pattern \u662f\u8981\u5339\u914d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32\u3002<\/li>\n<li>flags \u662f\u53ef\u9009\u9879\uff0c\u7528\u4e8e\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\u4e3a\uff08\u4f8b\u5982\u5927\u5c0f\u5199\u654f\u611f\u3001\u5168\u5c40\u641c\u7d22\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u56fe\u6848\u8bed\u6cd5<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\u5143\u5b57\u7b26\u548c\u6a21\u5f0f\u9650\u5b9a\u7b26\u6765\u5339\u914d\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5143\u5b57\u7b26\uff1a<\/p>\n<ul>\n<li>. \u5339\u914d\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u4efb\u4f55\u5b57\u7b26\u3002<\/li>\n<li>* \u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<li>+ \u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<li>? \u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u4e00\u6b21\u3002<\/li>\n<li>^ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u3002<\/li>\n<li>$ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002<\/li>\n<li>[] \u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<p><strong>\u6a21\u5f0f\u9650\u5b9a\u7b26<\/strong><\/p>\n<p>\u6a21\u5f0f\u9650\u5b9a\u7b26\u53ef\u4ee5\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\u4e3a\uff1a<\/p>\n<ul>\n<li>g \u5168\u5c40\u641c\u7d22\uff1a\u5339\u914d\u6240\u6709\u51fa\u73b0\u7684\u6a21\u5f0f\u3002<\/li>\n<li>i \u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff1a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<li>m \u591a\u884c\uff1a^ \u548c $ \u4e5f\u5339\u914d\u6362\u884c\u7b26\u3002<\/li>\n<li>s \u70b9\u8bed\u6cd5\uff1a. \u4e5f\u5339\u914d\u6362\u884c\u7b26\u3002<\/li>\n<li>u Unicode\uff1a\u542f\u7528 Unicode \u7f16\u7801\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<ul>\n<li>test()\uff1a\u6d4b\u8bd5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5b58\u5728\u6a21\u5f0f\u3002<\/li>\n<li>exec()\uff1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4fe1\u606f\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5339\u914d\u9879\u5219\u8fd4\u56de null\u3002<\/li>\n<li>match()\uff1a\u8fd4\u56de\u6240\u6709\u5339\u914d\u9879\u7684\u6570\u7ec4\u3002<\/li>\n<li>replace()\uff1a\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u5339\u914d\u7684\u6587\u672c\u3002<\/li>\n<li>search()\uff1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5339\u914d\u9879\u5219\u8fd4\u56de -1\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u8981\u67e5\u627e\u4ee5 &#8220;a&#8221; \u5f00\u5934\u4e14\u4ee5 &#8220;b&#8221; \u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<pre>const regex = \/a.*b\/;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8981\u67e5\u627e\u540c\u65f6\u5305\u542b &#8220;a&#8221; \u548c &#8220;b&#8221; \u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<pre>const regex = \/a.*b|b.*a\/;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 regexp \u5bf9\u8c61\u6765\u521b\u5efa\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\u5143\u5b57\u7b26\u548c\u6a21\u5f0f\u9650\u5b9a\u7b26\u6765\u5339\u914d\u6587\u672c\u3002\u5e38\u89c1\u5143\u5b57\u7b26\u5305\u62ec .\uff08\u5339\u914d\u9664\u6362\u884c\u7b26\u5916\u7684\u4efb\u4f55\u5b57\u7b26\uff09\u3001*\uff08\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u591a\u6b21\uff09\u3001+\uff08\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\uff09\u3001?\uff08\u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u4e00\u6b21\uff09\u3001^\uff08\u5339\u914d\u5b57\u7b26\u4e32\u5f00\u5934\uff09\u3001$\uff08\u5339\u914d\u5b57\u7b26\u4e32\u7ed3\u5c3e\uff09\u3001[]\uff08\u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\uff09\u3002\u6a21\u5f0f\u9650\u5b9a\u7b26\u5305\u62ec g\uff08\u5168\u5c40\u641c\u7d22\uff09\u3001i\uff08\u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff09\u3001m\uff08\u591a\u884c\uff09\u3001s\uff08\u70b9\u8bed\u6cd5\uff09\u548c u\uff08unicode\uff09\u3002\u53ef\u4ee5\u4f7f\u7528 test()\u3001exec()\u3001match() \u5982\u4f55\u4f7f\u7528 JavaScript \u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f \u6b63\u5219\u8868\u8fbe\u5f0f (regex) \u662f\u4e00\u79cd\u6a21\u5f0f\u5339\u914d\u8bed\u8a00\uff0c\u5b83\u7528\u4e8e\u67e5\u627e\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 RegExp \u5bf9\u8c61\u6765\u521b\u5efa\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u8bed\u6cd5 const regex = new RegExp(pattern, flags); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a pattern \u662f\u8981\u5339\u914d\u7684\u6a21\u5f0f\u5b57\u7b26\u4e32\u3002 flags \u662f\u53ef\u9009\u9879\uff0c\u7528\u4e8e\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\u4e3a\uff08\u4f8b\u5982\u5927\u5c0f\u5199\u654f\u611f\u3001\u5168\u5c40\u641c\u7d22\uff09\u3002 \u56fe\u6848\u8bed\u6cd5 \u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528\u5143\u5b57\u7b26\u548c\u6a21\u5f0f\u9650\u5b9a\u7b26\u6765\u5339\u914d\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5143\u5b57\u7b26\uff1a . \u5339\u914d\u9664\u6362\u884c\u7b26\u4e4b\u5916\u7684\u4efb\u4f55\u5b57\u7b26\u3002 * \u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u591a\u6b21\u3002 + \u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\u3002 ? \u5339\u914d\u524d\u9762\u7684\u5b57\u7b26\u96f6\u6b21\u6216\u4e00\u6b21\u3002 ^ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u3002 $ \u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002 [] \u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\u3002 \u6a21\u5f0f\u9650\u5b9a\u7b26 \u6a21\u5f0f\u9650\u5b9a\u7b26\u53ef\u4ee5\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\u4e3a\uff1a g \u5168\u5c40\u641c\u7d22\uff1a\u5339\u914d\u6240\u6709\u51fa\u73b0\u7684\u6a21\u5f0f\u3002 i \u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff1a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002 m [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3262","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3262"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3262\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}