{"id":147,"date":"2024-12-10T23:24:33","date_gmt":"2024-12-10T15:24:33","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=147"},"modified":"2024-12-10T23:24:33","modified_gmt":"2024-12-10T15:24:33","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-jquery-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d%e9%9d%9e-ascii-%e5%ad%97%e7%ac%a6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/10\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-jquery-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d%e9%9d%9e-ascii-%e5%ad%97%e7%ac%a6%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript\/jQuery \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u975e ASCII \u5b57\u7b26\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u8981\u5339\u914d\u975e ASCII \u5b57\u7b26\uff08\u5373 Unicode \u7f16\u7801\u8303\u56f4\u4e4b\u5916\u7684\u5b57\u7b26\uff0cASCII \u5b57\u7b26\u7f16\u7801\u8303\u56f4\u662f <code>0x00<\/code> \u5230 <code>0x7F<\/code>\uff0c\u4e5f\u5c31\u662f\u5341\u8fdb\u5236\u7684 0 \u5230 127\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 JavaScript \u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684 Unicode \u5b57\u7b26\u96c6\u652f\u6301\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6848 1\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JavaScript \u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u8303\u56f4\u8868\u793a\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>[\\x00-\\x7F]<\/code> \u8868\u793a ASCII \u5b57\u7b26\u8303\u56f4\uff0c\u56e0\u6b64\u975e ASCII \u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u53cd\u5411\u5339\u914d <code>[^]<\/code> \u8868\u793a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5339\u914d\u975e ASCII \u5b57\u7b26<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const nonAsciiPattern = \/&#91;^\\x00-\\x7F]\/g;\nconst text = \"Hello, \u4e16\u754c!\";\nconst nonAsciiChars = text.match(nonAsciiPattern);\n\nconsole.log(nonAsciiChars); \/\/ \u8f93\u51fa &#91;\"\u4e16\", \"\u754c\"]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6848 2\uff1a\u4f7f\u7528 Unicode \u5c5e\u6027\u8f6c\u4e49\uff08\u73b0\u4ee3\u65b9\u6cd5\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece ECMAScript 2018\uff08ES9\uff09\u5f00\u59cb\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301 Unicode \u5c5e\u6027\u8f6c\u4e49\uff0c\u901a\u8fc7 <code>\\p{}<\/code> \u8868\u793a Unicode \u5c5e\u6027\u3002\u8981\u5339\u914d\u975e ASCII \u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>\\P{ASCII}<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5339\u914d\u975e ASCII \u5b57\u7b26<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const nonAsciiPattern = \/\\P{ASCII}\/gu; \/\/ \u6ce8\u610f\u9700\u8981\u542f\u7528 `u` \u4fee\u9970\u7b26\u652f\u6301 Unicode\nconst text = \"Hello, \u4e16\u754c!\";\nconst nonAsciiChars = text.match(nonAsciiPattern);\n\nconsole.log(nonAsciiChars); \/\/ \u8f93\u51fa &#91;\"\u4e16\", \"\u754c\"]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5173\u4e8e <code>u<\/code> \u4fee\u9970\u7b26<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>u<\/code> \u4fee\u9970\u7b26\u662f Unicode \u6a21\u5f0f\u7684\u6807\u5fd7\uff0c\u7528\u4e8e\u6b63\u786e\u89e3\u6790\u548c\u5339\u914d Unicode \u5b57\u7b26\uff0c\u7279\u522b\u662f\u6d89\u53ca\u5230\u4ee3\u7406\u5bf9\uff08Surrogate Pairs\uff09\u7684\u5b57\u7b26\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u533a\u522b<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>[\\x00-\\x7F]<\/code> \u8303\u56f4\u7684\u65b9\u5f0f\u517c\u5bb9\u6027\u66f4\u597d\uff0c\u9002\u7528\u4e8e\u8001\u7248\u672c\u7684 JavaScript \u5f15\u64ce\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>\\P{ASCII}<\/code> \u5c5e\u6027\u8f6c\u4e49\u65b9\u5f0f\u66f4\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u8f83\u65b0\u7248\u672c\u7684 JavaScript \u5f15\u64ce\u652f\u6301\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f\u4e3e\u4f8b<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u975e ASCII \u5b57\u7b26<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const containsNonAscii = text =&gt; \/&#91;^\\x00-\\x7F]\/.test(text);\n\nconsole.log(containsNonAscii(\"Hello, World!\")); \/\/ false\nconsole.log(containsNonAscii(\"\u4f60\u597d\uff0c\u4e16\u754c!\"));     \/\/ true<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u66ff\u6362\u6240\u6709\u975e ASCII \u5b57\u7b26<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const text = \"Hello, \u4e16\u754c!\";\nconst replacedText = text.replace(\/&#91;^\\x00-\\x7F]\/g, '*');\n\nconsole.log(replacedText); \/\/ \u8f93\u51fa \"Hello, **!\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u63d0\u53d6\u6240\u6709\u975e ASCII \u5b57\u7b26<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const text = \"Data: \u6837\u672c\u6570\u636e123.\";\nconst nonAsciiChars = text.match(\/&#91;^\\x00-\\x7F]\/g) || &#91;];\n\nconsole.log(nonAsciiChars); \/\/ \u8f93\u51fa &#91;\"\u6837\", \"\u672c\", \"\u6570\", \"\u636e\"]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684 <code>[^]<\/code> \u662f\u53cd\u5411\u5339\u914d\u7b26\u3002<\/li>\n\n\n\n<li><code>\\x00-\\x7F<\/code> \u6307\u7684\u662f\u5355\u5b57\u8282 ASCII \u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u591a\u5b57\u8282\u7684 Unicode \u5b57\u7b26\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>\\P{}<\/code> \u5c5e\u6027\u8f6c\u4e49\u65f6\u5fc5\u987b\u5f00\u542f <code>u<\/code> \u4fee\u9970\u7b26\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u9700\u8981\u517c\u5bb9\u6027\uff1a\u63a8\u8350\u4f7f\u7528 <code>[^\\x00-\\x7F]<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u73af\u5883\u652f\u6301\u73b0\u4ee3\u7279\u6027\uff1a\u63a8\u8350\u4f7f\u7528 <code>\\P{ASCII}<\/code>\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5339\u914d\u975e ASCII \u5b57\u7b26\uff08\u5373 Unicode \u7f16\u7801\u8303\u56f4\u4e4b\u5916\u7684\u5b57\u7b26\uff0cASCII \u5b57\u7b26\u7f16\u7801\u8303\u56f4\u662f 0x00 \u5230  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}