{"id":169894,"date":"2024-05-08T16:56:03","date_gmt":"2024-05-08T08:56:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/169894.html"},"modified":"2024-05-08T16:56:07","modified_gmt":"2024-05-08T08:56:07","slug":"javascript-%e7%9a%84%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e4%b8%ad%e7%9a%84-b-%e4%bb%a5%e5%8f%8a-b-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/169894.html","title":{"rendered":"JavaScript \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 \\\\b \u4ee5\u53ca \\\\B \u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27040454\/627ec4bd-3754-4d25-8896-df278c93481b.webp\" alt=\"JavaScript \u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 \\\\b \u4ee5\u53ca \\\\B \u95ee\u9898\" \/><\/p>\n<p><p>\u5728JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c<code>\\b<\/code>\u548c<code>\\B<\/code>\u662f\u4e24\u4e2a\u91cd\u8981\u4e14\u529f\u80fd\u76f8\u53cd\u7684\u8fb9\u754c\u5339\u914d\u5b57\u7b26\u3002<strong><code>\\b<\/code>\u8868\u793a\u5355\u8bcd\u8fb9\u754c\u3001\u800c<code>\\B<\/code>\u8868\u793a\u975e\u5355\u8bcd\u8fb9\u754c<\/strong>\u3002\u7406\u89e3\u8fd9\u4e24\u4e2a\u5b57\u7b26\u7684\u7528\u9014\u548c\u533a\u522b\uff0c\u5bf9\u4e8e\u7f16\u5199\u6709\u6548\u4e14\u9ad8\u6548\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p><code>\\b<\/code>\u786e\u4fdd\u5339\u914d\u7684\u5b57\u7b26\u4e32\u4f4d\u4e8e\u5355\u8bcd\u7684\u5f00\u59cb\u6216\u7ed3\u675f\u5904\u3002\u5728JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\u5355\u8bcd\u88ab\u5b9a\u4e49\u4e3a\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u7ec4\u6210\u7684\u5b57\u7b26\u5e8f\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>\\b<\/code>\u5339\u914d &quot;is&quot; \u4f1a\u627e\u5230 &quot;This is a test&quot; \u4e2d\u7684 &quot;is&quot;\uff0c\u4f46\u4e0d\u4f1a\u5339\u914d &quot;This&quot; \u4e2d\u7684 &quot;is&quot;\uff0c\u56e0\u4e3a\u5728\u540e\u8005\u4e2d &quot;is&quot; \u4e0d\u662f\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u5355\u8bcd\u51fa\u73b0\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3<code>\\b<\/code>\u7684\u4f5c\u7528<\/h3>\n<\/p>\n<p><p><code>\\b<\/code>\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u786e\u4fdd\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5230\u7684\u5185\u5bb9\u4f4d\u4e8e\u8bcd\u7684\u8fb9\u754c\uff0c\u8fd9\u91cc\u7684\u8bcd\u754c\u6307\u7684\u662f\u8bcd\u4e0e\u7a7a\u683c\u3001\u6807\u70b9\u6216\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u4e4b\u95f4\u7684\u8fb9\u754c\u3002\u8fd9\u5bf9\u4e8e\u7cbe\u786e\u5339\u914d\u6574\u4e2a\u5355\u8bcd\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5339\u914d\u6587\u672c\u4e2d\u72ec\u7acb\u51fa\u73b0\u7684\u5355\u8bcd\u201capple\u201d\uff0c\u800c\u4e0d\u662f\u201capplesauce\u201d\u4e2d\u7684\u4e00\u90e8\u5206\u65f6\uff0c<code>\\bapple\\b<\/code>\u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u4f1a\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><code>\\b<\/code>\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u6790\uff0c\u5224\u65ad\u5b57\u7b26\u4e4b\u95f4\u7684\u8fb9\u754c\u662f\u5426\u7b26\u5408\u5355\u8bcd\u4e0e\u975e\u5355\u8bcd\u5b57\u7b26\u7684\u4ea4\u754c\u3002\u8fd9\u4e2a\u7279\u6027\u4f7f\u5f97<code>\\b<\/code>\u5728\u5904\u7406\u4ee5\u5355\u8bcd\u4e3a\u5355\u4f4d\u7684\u6587\u672c\u641c\u7d22\u6216\u5206\u6790\u65f6\u683c\u5916\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u63a2\u7d22<code>\\B<\/code>\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u4e0e<code>\\b<\/code>\u76f8\u53cd\uff0c<code>\\B<\/code>\u786e\u4fdd\u5339\u914d\u7684\u5185\u5bb9\u4e0d\u4f4d\u4e8e\u5355\u8bcd\u8fb9\u754c\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u4f7f\u7528<code>\\B<\/code>\uff0c\u90a3\u4e48\u671f\u671b\u5339\u914d\u7684\u6a21\u5f0f\u5e94\u8be5\u5728\u8bcd\u7684\u5185\u90e8\uff0c\u800c\u975e\u8d77\u59cb\u6216\u7ed3\u675f\u5904\u3002\u5b83\u5728\u8fc7\u6ee4\u6216\u5339\u914d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u5e8f\u5217\u4e3a\u5b50\u5e8f\u5217\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u5e8f\u5217\u4e0d\u72ec\u7acb\u4e8e\u5176\u4ed6\u6587\u672c\u5b58\u5728\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u8981\u67e5\u627e\u90a3\u4e9b\u5305\u542b\u8fde\u5b57\u7b26\u7684\u590d\u5408\u8bcd\uff0c\u4f46\u53c8\u4e0d\u5e0c\u671b\u8fd9\u4e9b\u8fde\u5b57\u7b26\u4f4d\u4e8e\u8bcd\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u5904\uff0c<code>\\B-\\B<\/code>\u6b63\u5219\u8868\u8fbe\u5f0f\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u3002\u5b83\u80fd\u5e2e\u52a9\u6211\u4eec\u5339\u914d\u50cf\u201cco-op\u201d\u8fd9\u6837\u7684\u8bcd\uff0c\u4f46\u4e0d\u4f1a\u5339\u914d\u5230\u201c-break\u201d\u6216\u201cend-\u201d\u8fd9\u6837\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001<code>\\b<\/code>\u4e0e<code>\\B<\/code>\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u533a\u5206<code>\\b<\/code>\u548c<code>\\B<\/code>\u7684\u4f7f\u7528\u573a\u666f\u5bf9\u4e8e\u6784\u5efa\u9ad8\u6548\u4e14\u51c6\u786e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u81f3\u5173\u91cd\u8981\u3002\u5f53\u4f60\u9700\u8981\u786e\u4fdd\u641c\u7d22\u6216\u5339\u914d\u7684\u5b57\u7b26\u4e32\u51fa\u73b0\u5728\u8bcd\u7684\u8fb9\u754c\u65f6\uff0c<code>\\b<\/code>\u5c06\u662f\u4f60\u7684\u6700\u4f73\u9009\u62e9\u3002\u5b83\u975e\u5e38\u9002\u7528\u4e8e\u641c\u7d22\u6574\u4e2a\u5355\u8bcd\u6216\u786e\u4fdd\u5339\u914d\u5e8f\u5217\u5904\u4e8e\u72ec\u7acb\u5355\u8bcd\u7684\u8d77\u59cb\u6216\u7ec8\u6b62\u4f4d\u7f6e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u53cd\u4e4b\uff0c\u5f53\u4f60\u7684\u76ee\u6807\u662f\u5339\u914d\u4f4d\u4e8e\u5355\u8bcd\u5185\u90e8\u7684\u5e8f\u5217\u65f6\uff0c\u4f7f\u7528<code>\\B<\/code>\u4f1a\u66f4\u52a0\u5408\u9002\u3002\u5b83\u9002\u7528\u4e8e\u90a3\u4e9b\u9700\u8981\u5728\u5355\u8bcd\u7684\u4e2d\u95f4\u5bfb\u627e\u5339\u914d\uff0c\u800c\u4e0d\u5e0c\u671b\u5339\u914d\u4f4d\u4e8e\u5355\u8bcd\u8fb9\u754c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ee3\u7801\u793a\u4f8b\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u5177\u4f53\u8bf4\u660e<code>\\b<\/code>\u548c<code>\\B<\/code>\u7684\u7528\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>\\b<\/code>\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u72ec\u7acb\u7684\u201ccat\u201d\u5355\u8bcd\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">let text = &quot;The cat scattered his food all over the room.&quot;;<\/p>\n<p>let pattern = \/\\bcat\\b\/g;<\/p>\n<p>console.log(text.match(pattern)); \/\/ \u8f93\u51fa\uff1a[&quot;cat&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u51c6\u786e\u627e\u5230\u4e86\u4f5c\u4e3a\u72ec\u7acb\u5355\u8bcd\u201ccat\u201d\u7684\u5b9e\u4f8b\uff0c\u800c\u6ca1\u6709\u5c06\u201cscattered\u201d\u4e2d\u7684\u201ccat\u201d\u4f5c\u4e3a\u5339\u914d\u5185\u5bb9\u8fd4\u56de\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>\\B<\/code>\u67e5\u627e\u975e\u5355\u8bcd\u8fb9\u754c\u5904\u7684\u201ccat\u201d\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">let text = &quot;The cat scattered his food all over the catacomb.&quot;;<\/p>\n<p>let pattern = \/\\Bcat\\B\/g;<\/p>\n<p>console.log(text.match(pattern)); \/\/ \u8f93\u51fa\uff1a[&quot;cat&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53ea\u6709\u201ccatacomb\u201d\u4e2d\u7684\u201ccat\u201d\u88ab\u5339\u914d\u5230\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f4d\u4e8e\u5355\u8bcd\u8fb9\u754c\u5904\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u7406\u89e3\u5e76\u719f\u7ec3\u638c\u63e1<code>\\b<\/code>\u548c<code>\\B<\/code>\u7684\u533a\u522b\u4e0e\u5e94\u7528\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u4e0e\u9ad8\u8d28\u91cf\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bf4\u6781\u5176\u91cd\u8981\u3002\u8bb0\u4f4f\uff0c<code>\\b<\/code>\u7528\u4e8e\u5339\u914d\u4f4d\u4e8e\u5355\u8bcd\u8fb9\u754c\u7684\u5185\u5bb9\uff0c\u800c<code>\\B<\/code>\u5219\u7528\u4e8e\u5339\u914d\u4f4d\u4e8e\u975e\u5355\u8bcd\u8fb9\u754c\u7684\u5185\u5bb9\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u7684\u5339\u914d\u9700\u6c42\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e24\u4e2a\u7279\u6b8a\u5b57\u7b26\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5339\u914d\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662f JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 \\b \u548c \\B\uff1f<\/strong><br \/>\nJavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 \\b \u548c \\B \u662f\u7528\u6765\u5339\u914d\u5355\u8bcd\u8fb9\u754c\u7684\u7279\u6b8a\u5b57\u7b26\u3002\u5f53\u4f7f\u7528 \\b \u65f6\uff0c\u5b83\u5339\u914d\u4e00\u4e2a\u5355\u8bcd\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\uff0c\u800c \\B \u5339\u914d\u4e0d\u5728\u5355\u8bcd\u8fb9\u754c\u7684\u4efb\u610f\u4f4d\u7f6e\u3002\u8fd9\u4e24\u4e2a\u5b57\u7b26\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u5b57\u7b26\u4e32\u5339\u914d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528 \\b \u548c \\B\uff1f<\/strong><br \/>\n\u8981\u5728 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528 \\b \u548c \\B\uff0c\u53ea\u9700\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u8f6c\u4e49\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 \\b \u6765\u5339\u914d\u4ee5 &quot;hello&quot; \u5f00\u5934\u7684\u5355\u8bcd\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f \/\\bhello\\w*\/\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u4ee5\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 \\b \u548c \\B\uff1f<\/strong><br \/>\nJavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 \\b \u548c \\B \u5728\u5f88\u591a\u573a\u666f\u4e2d\u90fd\u6709\u5b9e\u9645\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 \\b \u6765\u5339\u914d\u6574\u4e2a\u5355\u8bcd\uff0c\u800c\u4e0d\u662f\u5355\u8bcd\u7684\u4e00\u90e8\u5206\uff1b\u8fd8\u53ef\u4ee5\u4f7f\u7528 \\B \u6765\u5339\u914d\u4e0d\u5728\u5355\u8bcd\u8fb9\u754c\u4e0a\u7684\u7279\u5b9a\u5b57\u7b26\u3002\u8fd9\u4e9b\u529f\u80fd\u53ef\u4ee5\u7528\u4e8e\u641c\u7d22\u548c\u66ff\u6362\u64cd\u4f5c\u3001\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u4ee5\u53ca\u6570\u636e\u5206\u6790\u7b49\u65b9\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\uff0c\\b\u548c\\B\u662f\u4e24\u4e2a\u91cd\u8981\u4e14\u529f\u80fd\u76f8\u53cd\u7684\u8fb9\u754c\u5339\u914d\u5b57\u7b26\u3002\\b\u8868\u793a\u5355\u8bcd\u8fb9\u754c\u3001\u800c\\B\u8868\u793a [&hellip;]","protected":false},"author":3,"featured_media":169898,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169894"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=169894"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/169894\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169898"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=169894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=169894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=169894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}