{"id":2311,"date":"2024-11-10T16:21:15","date_gmt":"2024-11-10T08:21:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2311\/"},"modified":"2024-11-10T16:21:15","modified_gmt":"2024-11-10T08:21:15","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8codemirror%e4%b8%ba%e5%8c%b9%e9%85%8d%e7%9a%84%e6%97%a5%e5%bf%97%e5%ad%97%e6%ae%b5%e6%b7%bb%e5%8a%a0%e7%89%b9%e5%ae%9a%e5%ad%97%e7%ac%a6%e6%a0%b7%e5%bc%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2311\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528CodeMirror\u4e3a\u5339\u914d\u7684\u65e5\u5fd7\u5b57\u6bb5\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u6837\u5f0f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173071244480168.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528CodeMirror\u4e3a\u5339\u914d\u7684\u65e5\u5fd7\u5b57\u6bb5\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u6837\u5f0f\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528CodeMirror\u4e3a\u5339\u914d\u7684\u65e5\u5fd7\u5b57\u6bb5\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u6837\u5f0f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>codemirror\u5982\u4f55\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u6837\u5f0f<\/strong><\/p>\n<p>\u5bf9\u4e8e\u9700\u8981\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u65e5\u5fd7\u5185\u5bb9\u5e76\u9ad8\u4eae\u5339\u914d\u5b57\u6bb5\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548cHTML\u3001CSS\u5b9e\u73b0\u3002\u534e\u4e3a\u4e91\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u7ed9\u5339\u914d\u5b57\u6bb5\u6dfb\u52a0span\u6807\u7b7e\u5e76\u8bbe\u7f6e\u6837\u5f0f\u3002<\/p>\n<p><strong>JavaScript \u5b9e\u73b0<\/strong><\/p>\n<pre>\/\/ \u5b9a\u4e49\u65e5\u5fd7\u5185\u5bb9\nconst logContent = \"2022-03-10 10:30:15 [INFO] User john logged in\";\n\n\/\/ \u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\nconst regex = \/User (w+) logged in\/;\n\n\/\/ \u5339\u914d\u5e76\u9ad8\u4eae\nconst match = logContent.match(regex);\nif (match) {\n  const highlighted = logContent.replace(regex, `&lt;span class=\"highlight\"&gt;$$&lt;\/span&gt;`);\n  console.log(highlighted);\n  \/\/ \u8f93\u51fa\uff1a2022-03-10 10:30:15 [INFO] &lt;span class=\"highlight\"&gt;User john logged in&lt;\/span&gt;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7528\u6237\u540d\u5b57\u6bb5\uff0c\u5e76\u7528span\u6807\u7b7e\u5305\u88f9\u5b83\uff0c\u540c\u65f6\u6dfb\u52a0CSS\u7c7b\u540d&#8221;highlight&#8221;\u3002<\/p>\n<p><strong>CSS \u6837\u5f0f<\/strong><\/p>\n<pre>.highlight {\n  background-color: yellow;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u4e3aCodeMirror\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6dfb\u52a0\u6837\u5f0f\uff0c\u5b9e\u73b0\u9ad8\u4eae\u663e\u793a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528CodeMirror\u4e3a\u5339\u914d\u7684\u65e5\u5fd7\u5b57\u6bb5\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u6837\u5f0f\uff1f\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>codemirror\u5982\u4f55\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u6837\u5f0f \u5bf9\u4e8e\u9700\u8981\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u65e5\u5fd7\u5185\u5bb9\u5e76\u9ad8\u4eae\u5339\u914d\u5b57\u6bb5\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548cHTML\u3001CSS\u5b9e\u73b0\u3002\u534e\u4e3a\u4e91\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u7ed9\u5339\u914d\u5b57\u6bb5\u6dfb\u52a0span\u6807\u7b7e\u5e76\u8bbe\u7f6e\u6837\u5f0f\u3002 JavaScript \u5b9e\u73b0 \/\/ \u5b9a\u4e49\u65e5\u5fd7\u5185\u5bb9 const logContent = &#8220;2022-03-10 10:30:15 [INFO] User john logged in&#8221;; \/\/ \u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f const regex = \/User (w+) logged in\/; \/\/ \u5339\u914d\u5e76\u9ad8\u4eae const match = logContent.match(regex); if (match) { const highlighted = logContent.replace(regex, `&lt;span class=&#8221;highlight&#8221;&gt;$$&lt;\/span&gt;`); console.log(highlighted); \/\/ \u8f93\u51fa\uff1a2022-03-10 10:30:15 [INFO] &lt;span class=&#8221;highlight&#8221;&gt;User john logged in&lt;\/span&gt; } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7528\u6237\u540d\u5b57\u6bb5\uff0c\u5e76\u7528span\u6807\u7b7e\u5305\u88f9\u5b83\uff0c\u540c\u65f6\u6dfb\u52a0CSS\u7c7b\u540d&#8221;highlight&#8221;\u3002 CSS \u6837\u5f0f [&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-2311","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2311","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=2311"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2311\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}