{"id":12076,"date":"2026-02-12T18:23:48","date_gmt":"2026-02-12T10:23:48","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12076"},"modified":"2026-02-12T18:23:48","modified_gmt":"2026-02-12T10:23:48","slug":"js-regexp-y-sticky-flags","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2026\/02\/js-regexp-y-sticky-flags\/","title":{"rendered":"JS\u6b63\u5219\u8868\u8fbe\u5f0fy\u6807\u8bc6\u7b26\u4e4b\u7c98\u6027\u5339\u914d"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12076\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12076<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u524d\u8a00\u6982\u8ff0<\/h3>\n<p>\u5f53\u5e74\u6211\u662f\u6367\u7740JavaScript\u9ad8\u7ea7\u8bed\u8a00\u8bbe\u8ba1\u8fd9\u672c\u4e66\u5b66\u4e60JS\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\uff0c\u77e5\u8bc6\u57fa\u672c\u4e0a\u90fd\u505c\u7559\u5728\u90a3\u4e2a\u65f6\u671f\u3002<\/p>\n<p>\u6700\u8fd1\u5076\u7136\u53d1\u73b0\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u652f\u6301sticky\u7c98\u6027\u6807\u8bc6\uff0c\u4f7f\u7528\u5b57\u6bcdy\u8868\u793a\u3002<\/p>\n<p>\u770b\u4e86\u4e0b\u652f\u6301\u7684\u65f6\u95f4\uff0c\u8ddd\u4eca\u4e5f\u6709\u4e94\u516d\u5e74\u7684\u65f6\u95f4\u4e86\uff0c\u5df2\u7ecf\u8c08\u4e0d\u4e0a\u65b0\u7279\u6027\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-12_172005.png\" width=\"655\" height=\"239\" alt=\"\u7c98\u6027\u6b63\u5219\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8d81\u7740\u6625\u8282\u524d\u6bd4\u8f83\u6709\u7a7a\uff0c\u8d76\u5feb\u5b66\u4e60\u4e00\u756a\u3002<\/p>\n<h3>\u4e8c\u3001y\u6807\u8bc6\u7b26\u57fa\u7840\u5e38\u8bc6<\/h3>\n<p>\u7c98\u6027\u5339\u914d\u7684\u6807\u8bc6\u7b26\u662f<code>y<\/code>\u3002<\/p>\n<p>\u987a\u4fbf\u56de\u987e\u4e0b\u5176\u4ed6\u6807\u8bc6\u7b26\uff0c\u5168\u5c40\u662f<code>g<\/code>\uff0c\u4e0d\u7f3a\u5206\u5927\u5c0f\u5199\u662f<code>i<\/code>\uff0c\u591a\u884c\u662f<code>m<\/code>\u3002<\/p>\n<p>\u4ee5\u4e0a\u77e5\u8bc6\u90fd\u662f\u6240\u6709\u524d\u7aef\u5f00\u53d1\u4eba\u5458\u90fd\u9700\u8981\u638c\u63e1\u7684\u3002<\/p>\n<h4>\u5fc5\u4e0d\u53ef\u5c11\u7684lastIndex<\/h4>\n<p>\u7c98\u6027\u5339\u914d\u5728\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u4e00\u5b9a\u8981\u6307\u5b9alastIndex\uff0c\u56e0\u4e3a\u4ed6\u7684\u542b\u4e49\u5c31\u662f\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u5339\u914d\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const str = \"table football\";\r\nconst regex = \/foo\/y;\r\n\r\nregex.lastIndex = 6;\r\n\r\nconsole.log(regex.test(str));\r\n\/\/ \u8f93\u51fa\u7ed3\u679c\u662f\uff1a true\r\n\r\nconsole.log(regex.test(str));\r\n\/\/ \u8f93\u51fa\u7ed3\u679c\u662f: false<\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u610f\u4ee3\u7801\uff0c\u7b2c\u4e00\u4e2a<code>regex.test(str)<\/code>\u4e4b\u6240\u4ee5\u4e3a<code>true<\/code>\uff0c\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32<code>\"table football\"<\/code>\u7684\u7d22\u5f156\u4f4d\u7f6e\u662f\u7a7a\u683c\uff0c\u6b63\u597d\u540e\u9762\u7684\u5b57\u7b26\u5c31\u662f <code>foo<\/code>\u3002<\/p>\n<p>\u800c\u7b2c\u4e8c\u4e2a<code>regex.test(str)<\/code>\u8fd4\u56de\u503c\u662f<code>false<\/code>\u662f\u56e0\u4e3a\u7c98\u6027\u5339\u914d\u5b8c\u6210\u540e\uff0c\u5982\u679c\u5339\u914d\uff0c\u5219lastIndex\u81ea\u52a8\u5b9a\u4f4d\u5230\u5339\u914d\u5b57\u7b26\u7684\u7ed3\u5c3e\uff0c\u4e5f\u5c31\u662f<code>tball<\/code>\uff0c\u81ea\u7136\u5c31\u8fd4\u56de<code>false<\/code>\u3002<\/p>\n<p>\u5982\u679c\u7c98\u6027\u5b9a\u4f4d\u5339\u914d\u5931\u8d25\uff0c\u90a3\u4e48<code>lastIndex<\/code>\u4f1a\u53d8\u62100.<\/p>\n<p>\u4e0b\u56fe\u5c31\u662f\u8fd0\u884c\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-12_173428.png\" width=\"372\" height=\"190\" alt=\"\u7c98\u6027\u5b9a\u4f4d\u8fd0\u884c\u7ed3\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001\u4f7f\u7528\u7c98\u6027\u5339\u914d\u7684\u573a\u666f<\/h3>\n<p>\u7c98\u6027\u5339\u914d<code>y<\/code>\u6807\u8bc6\u7b26\u9002\u5408\u5177\u6709\u89c4\u5f8b\u7ed3\u6784\u7684\u590d\u6742\u5b57\u7b26\u4e32\u5339\u914d\u3002<\/p>\n<p>\u4f8b\u5982\u89e3\u6790 Token\uff08\u6807\u8bb0\u5316\uff09\u3001\u6784\u5efa\u8bcd\u6cd5\u5206\u6790\u5668\u3001\u89e3\u6790\u7279\u5b9a\u683c\u5f0f\u6570\u636e\u6d41\u3002 <\/p>\n<p>\u4e0b\u9762\u4ee5\u89e3\u6790\u4e00\u6bb5\u7b80\u5355\u7684 CSS \u58f0\u660e\u5757\u793a\u610f\uff1a<\/p>\n<pre>const cssInput = \"color: #fff; display: block; margin: 20px;\";\r\n\r\n\/\/ \u5b9a\u4e49 Sticky \u6b63\u5219\r\n\/\/ \u5339\u914d \"\u5c5e\u6027\u540d: \u503c;\" \u8fd9\u79cd\u7ed3\u6784\uff0c\u5e76\u5141\u8bb8\u5c5e\u6027\u540d\u524d\u540e\u6709\u53ef\u9009\u7a7a\u683c\r\nconst propRegex = \/\\s*([a-z-]+)\\s*:\\s*([^;]+)\\s*;\/y;\r\n\r\nfunction parseCSS(input) {\r\n  const declarations = [];\r\n  \r\n  \/\/ \u53ea\u8981\u5339\u914d\u6210\u529f\uff0cpropRegex.lastIndex \u5c31\u4f1a\u81ea\u52a8\u66f4\u65b0\u5230\u4e0b\u4e00\u6b21\u5339\u914d\u7684\u8d77\u70b9\r\n  while (true) {\r\n    const match = propRegex.exec(input);\r\n    \r\n    if (match) {\r\n      const [fullMatch, property, value] = match;\r\n      declarations.push({ property, value: value.trim() });\r\n    } else {\r\n      \/\/ \u68c0\u67e5\u662f\u5426\u662f\u56e0\u4e3a\u89e3\u6790\u5230\u4e86\u672b\u5c3e\u800c\u505c\u6b62\uff0c\u8fd8\u662f\u56e0\u4e3a\u9047\u5230\u4e86\u975e\u6cd5\u683c\u5f0f\r\n      if (propRegex.lastIndex &lt; input.length) {\r\n        console.warn(`\u89e3\u6790\u4e2d\u65ad\uff0c\u5269\u4f59\u5185\u5bb9\u4e0d\u7b26\u5408 CSS \u683c\u5f0f\u3002`);\r\n      }\r\n      break;\r\n    }\r\n  }\r\n  \r\n  return declarations;\r\n}\r\n\r\nconst result = parseCSS(cssInput);\r\nconsole.table(result);<\/pre>\n<p>\u8f93\u51fa\u7684\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-12_175357.png\" width=\"395\" height=\"175\" alt=\"CSS\u89e3\u6790\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u6781\u81f4\u7684\u6027\u80fd\u4f18\u5316<\/h4>\n<p>\u5728\u5904\u7406\u957f\u6587\u672c\u65f6\uff0cSticky \u6a21\u5f0f\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<p>\u6211\u4eec\u4e0d\u59a8\u5047\u8bbe\u4e00\u4e2a\u573a\u666f\uff0c\u5728\u8fd9\u4e2a\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u5df2\u77e5\u76ee\u6807\u5185\u5bb9\u5e94\u8be5\u51fa\u73b0\u5728\u7d22\u5f15 n \u5904\u3002<\/p>\n<p>\u6b64\u65f6\u53ef\u4ee5\u5bf9\u6bd4\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5168\u5c40\u6a21\u5f0f (\/pattern\/g)\uff1a \u5982\u679c\u4f4d\u7f6e n \u4e0d\u5339\u914d\uff0c\u5f15\u64ce\u4f1a\u7ee7\u7eed\u626b\u63cf n+1, n+2 \u76f4\u5230\u6587\u672c\u7ed3\u675f\u3002<\/li>\n<li>Sticky \u6a21\u5f0f (\/pattern\/y)\uff1a \u5982\u679c\u4f4d\u7f6e n \u4e0d\u5339\u914d\uff0c\u7acb\u5373\u505c\u6b62\u5e76\u8fd4\u56de null\u3002\u8fd9\u907f\u514d\u4e86\u5728\u5927\u89c4\u6a21\u6587\u672c\u4e2d\u8fdb\u884c\u65e0\u8c13\u7684\u5168\u91cf\u626b\u63cf\u3002 <\/li>\n<\/ul>\n<h4>\u6a21\u62df\u951a\u70b9\u5339\u914d<\/h4>\n<p>\u8fd9\u4e2a\u573a\u666f\u2026\u2026\u4e5f\u7b97\u4e0d\u4e0a\u4ec0\u4e48\u4f18\u52bf\uff0c\u53ea\u80fd\u8bf4\u662f\u4e2a\u989d\u5916\u5b9e\u73b0\u6280\u5de7\u3002<\/p>\n<p>\u5728\u975e\u591a\u884c\u6a21\u5f0f\u4e0b\uff0c<code>lastIndex<\/code>\u4e3a<code>0<\/code>\u7684Sticky\u6b63\u5219\u5176\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u5e26\u4e86\u884c\u9996\u951a\u70b9 ^ \u7684\u6b63\u5219\u3002 <\/p>\n<p>\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5f3a\u5236\u6b63\u5219\u4ece\u5934\u5f00\u59cb\u5339\u914d\uff0c\u4e14\u4e0d\u5e0c\u671b\u5728\u6b63\u5219\u5b57\u7b26\u4e32\u91cc\u786c\u7f16\u7801 ^\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>y<\/code> \u6807\u5fd7\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>\/^\\d+\/<\/pre>\n<p>\u53ef\u4ee5\u5199\u6210\uff1a<\/p>\n<pre>\/\\d+\/y<\/pre>\n<h3>\u56db\u3001\u5176\u4ed6\u4e9b\u8865\u5145\u53ca\u7ed3\u8bed<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u501f\u52a9RegExp.prototype.sticky\u5224\u65ad\u4e00\u4e2a\u6b63\u5219\u662f\u4e0d\u662f\u7c98\u6027\u5339\u914d\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const regex = \/foo\/y;\r\nconsole.log(regex.sticky);\r\n\/\/ \u8fd4\u56de\u7ed3\u679c: true<\/pre>\n<p>\u60f3\u60f3\u770b\uff0c\u8fd8\u6709\u6ca1\u6709\u5176\u4ed6\u9057\u6f0f\u7684\u3002<\/p>\n<p>\u54e6\uff0c\u6709\u4e2a\u7ec6\u8282\uff0c\u5c31\u662f<code>exec()<\/code>\u548c<code>test()<\/code>\u65b9\u6cd5\u7684\u4e00\u4e2a\u5dee\u5f02\uff0c\u6309\u7167MDN\u6587\u6863\u7684\u8bf4\u6cd5\uff1a<\/p>\n<blockquote><p>\u5bf9\u4e8eexec()\u65b9\u6cd5\uff0c\u540c\u65f6\u5177\u6709\u7c98\u6027\uff08sticky\uff09\u548c\u5168\u5c40\uff08global\uff09\u7279\u6027\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u540c\u65f6\u5177\u6709\u7c98\u6027\u548c\u975e\u5168\u5c40\u7279\u6027\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u884c\u4e3a\u76f8\u540c\u3002\u7531\u4e8etest()\u662fexec()\u7684\u7b80\u5355\u5c01\u88c5\uff0c\u56e0\u6b64\u5b83\u4f1a\u5ffd\u7565\u5168\u5c40\u6807\u5fd7\uff0c\u540c\u6837\u6267\u884c\u7c98\u6027\u5339\u914d\u3002<\/p><\/blockquote>\n<p>\u5c31\u6211\u4e2a\u4eba\u800c\u8a00\uff0c<code>exec()<\/code>\u65b9\u6cd5\u5f88\u5c11\u4f7f\u7528\uff0c\u6240\u4ee5\uff0c\u4e0a\u9762\u7684\u7ec6\u8282\u5dee\u5f02\uff0c\u6211\u4e5f\u61d2\u5f97\u6df1\u7a76\u4e86\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\u5427\uff0c\u6211\u4eec\u6625\u8282\u540e\u518d\u89c1\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-12_181701.jpeg\" width=\"116\" height=\"177\" alt=\"\u6625\u8282\u5feb\u4e50\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12076\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12076<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202602\/2026-2-12_182147.jpeg\" width=\"160\" height=\"96\" alt=\"y\u6807\u8bc6\u7b26\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u6211\u6700\u8fd1\u624d\u77e5\u9053\uff0cJS\u6b63\u5219\u9664\u4e86\u5168\u5c40g\uff0c\u4e0d\u7f3a\u5206\u5927\u5c0f\u5199i\uff0c\u6362\u884cm\uff0c\u8fd8\u652f\u6301\u4e00\u4e2a\u540d\u4e3ay\u7684\u6807\u8bc6\u7b26\uff0c\u8868\u793a\u7c98\u6027\u5339\u914d\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1849,2105,801,1828,1384,222,1564],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12076"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=12076"}],"version-history":[{"count":4,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12076\/revisions"}],"predecessor-version":[{"id":12093,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12076\/revisions\/12093"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=12076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=12076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=12076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}