{"id":616,"date":"2008-10-26T10:10:41","date_gmt":"2008-10-26T02:10:41","guid":{"rendered":"http:\/\/fdream.net\/blog\/article\/616.aspx"},"modified":"2008-10-27T10:10:28","modified_gmt":"2008-10-27T02:10:28","slug":"Mootools+1.2%e6%95%99%e7%a8%8b(9)%e2%80%94%e2%80%94%e8%be%93%e5%85%a5%e8%bf%87%e6%bb%a4%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%88%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/616","title":{"rendered":"Mootools 1.2\u6559\u7a0b(9)\u2014\u2014\u8f93\u5165\u8fc7\u6ee4\u7b2c\u4e8c\u90e8\u5206\uff08\u5b57\u7b26\u4e32\uff09"},"content":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.consideropen.com\/blog\/2008\/08\/30-days-of-mootools-12-tutorials-day-9-input-filtering-strings\/\" target=\"_blank\" title=\"30 Days of Mootools 1.2 Tutorials - Day 9 - Input Filtering Part II (Strings)\">30 Days of Mootools 1.2 Tutorials &#8211; Day 9 &#8211; Input Filtering Part II (Strings)<\/a><\/p>\n<h2>\u8f93\u5165\u8fc7\u6ee4\u7b2c\u4e8c\u90e8\u5206\uff08\u5b57\u7b26\u4e32\uff09<\/h2>\n<p>\u8bf7\u5c0a\u91cd\u4e2a\u4eba\u52b3\u52a8\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttp:\/\/fdream.net\uff0c \u8bd1\u8005\uff1aFdream<\/p>\n<h3>\u5b57\u7b26\u4e32\u51fd\u6570<\/h3>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u51c6\u5907\u597d\u5f00\u59cb\u8fd9\u4e00\u8bb2\uff0c\u8bf7\u53c2\u8003\u8fd9\u4e00\u7cfb\u5217\u7684\u6559\u7a0b\uff0c\u8fd9\u91cc\u662f<a href=\"http:\/\/fdream.net\/blog\/article\/605.aspx\" target=\"_blank\" title=\"\u300aMooTools 1.2\u7cfb\u5217\u6559\u7a0b\u76ee\u5f55\u300b\">\u300aMooTools 1.2\u7cfb\u5217\u6559\u7a0b\u76ee\u5f55\u300b<\/a>\u3002<\/p>\n<p>\u4eca\u5929\u6211\u4eec\u6765\u770b\u4e00\u770bMooTools\u7ed9\u6211\u4eec\u63d0\u4f9b\u7684\u989d\u5916\u7684\u4e00\u4e9b\u5904\u7406\u5b57\u7b26\u51fd\u6570\u3002\u8fd9\u53ea\u662fMooTools\u5b57\u7b26\u4e32\u5904\u7406\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u4e0d\u5305\u542b\u4e00\u4e9b\u795e\u79d8\u7684\u51fd\u6570\uff08\u6bd4\u5982toCamelCase()\uff09\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728\u4ee5\u540e\u53e6\u5916\u7528\u4e00\u8bb2\u6765\u5c06\u4e00\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u77e5\u8bc6\u548c\u5728MooTools\u4e0b\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u60f3\u5148\u82b1\u4e00\u70b9\u65f6\u95f4\u6765\u770b\u4e00\u4e0b\u5b57\u7b26\u4e32\u51fd\u6570\u662f\u600e\u4e48\u8c03\u7528\u7684\u3002\u5728\u6211\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u662f\u5728\u5b57\u7b26\u4e32\u53d8\u91cf\u4e0a\u9762\u76f4\u63a5\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\uff0c\u5c31\u50cf\u4e0b\u9762\u7684\u8fd9\u6837\uff1a<\/p>\n<p><pre lang=\"javascript\">\nvar my_text_variable = &quot;Heres some text&quot;;\n\/\/  \u7ed3\u679c                 \u5b57\u7b26\u4e32\u53d8\u91cf        \u65b9\u6cd5\u540d\nvar result_of_function = my_text_variable.someStringFunction();\n<\/pre>\n<\/p>\n<p>\u4f46\u662f\u6211\u8fd9\u6837\u5199\u53ea\u662f\u4e3a\u4e86\u80fd\u591f\u66f4\u6e05\u695a\u5730\u89e3\u91ca\u5b83\uff0c\u4f60\u5e94\u8be5\u4e86\u89e3\u5230\u8fd9\u4e9b\u5b57\u7b26\u4e32\u51fd\u6570\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e0a\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<p><pre lang=\"javascript\">\nvar result_of_function = &quot;Heres some text&quot;.someStringFunction();\n<\/pre>\n<\/p>\n<p>\u6ce8\u610f\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u65b9\u5f0f\u5728MooTools\u4e2d\u7684\u6570\u5b57\u5904\u7406\u51fd\u6570\u4e5f\u540c\u6837\u6709\u6548\uff1a<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u6ce8\u610f\u4e00\u4e0b\u7528\u6cd5\uff0c\u662f\u62ec\u53f7\u4e2d\u7684\u6570\u5b57\n\/\/ \u800c\u4e0d\u662f\u5355\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u5b57\u7b26\u4e32\nvar limited_number = (256).limit(1, 100);\n<\/pre>\n<\/p>\n<p>\u8fd8\u6709\uff0c\u6211\u60f3\u518d\u6b21\u5f3a\u8c03\u4e00\u904d\uff1a\u7528JavaScript\u5bf9\u8f93\u5165\u8fc7\u6ee4\u5e76\u4e0d\u80fd\u5728\u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u5b89\u5168\u8fc7\u6ee4\u3002\u4f60\u5728JavaScript\u4e2d\u5199\u7684\u6240\u6709\u7684\u4e00\u5207\u90fd\u53ef\u4ee5\u88ab\u4f60\u7684\u7f51\u9875\u6d4f\u89c8\u8005\u770b\u5230\u3001\u64cd\u63a7\u548c\u7981\u6b62\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u8bb2MooTools\u7684Request\u7c7b\u65f6\uff0c\u5bf9PHP\u7684\u8fc7\u6ee4\u6280\u672f\u8fdb\u884c\u4e00\u4e9b\u7b80\u5355\u7684\u63a2\u8ba8\u3002\u540c\u65f6\uff0c\u7ee7\u7eed\u4fdd\u6301\u539f\u6765\u8981\u5728\u670d\u52a1\u5668\u7aef\u505a\u7684\u4efb\u4f55\u4e0e\u5b89\u5168\u76f8\u5173\u7684\u4e8b\u60c5\uff0c\u4e0d\u8981\u4f9d\u8d56JavaScript\u3002<\/p>\n<h3>trim()<\/h3>\n<p>trim\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\u6765\u53bb\u6389\u4efb\u4f55\u4f60\u60f3\u5904\u7406\u7684\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u8fd9\u662f\u6211\u4eec\u8981trim\u7684\u5b57\u7b26\u4e32\nvar text_to_trim =  &quot;    \\nString With Whitespace     &quot;;\n\/\/ trim\u540e\u7684\u5b57\u7b26\u4e32\u662f&quot;String With Whitespace&quot;\nvar trimmed_text = text_to_trim.trim();\n<\/pre>\n<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u89c1\u8fc7\\n\uff0c\u5176\u5b9e\u8fd9\u53ea\u662f\u4e00\u4e2a\u6362\u884c\u7b26\u800c\u5df2\u3002\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5b83\u6765\u628a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u884c\u3002trim\u65b9\u6cd5\u628a\u6362\u884c\u7b26\u4e5f\u5f53\u4f5c\u4e00\u4e2a\u7a7a\u767d\u7b26\uff0c\u56e0\u6b64\u5b83\u4e5f\u4f1a\u628a\u6362\u884c\u7b26\u53bb\u6389\u3002trim\u65b9\u6cd5\u552f\u4e00\u4e0d\u505a\u7684\u4e00\u4ef6\u7279\u522b\u7684\u4e8b\u60c5\u5c31\u662f\uff1a\u5b83\u5e76\u4e0d\u4f1a\u53bb\u6389\u4e00\u4e2a\u5b57\u7b26\u4e32\u91cc\u9762\u7684\u4efb\u4f55\u591a\u4f59\u7684\u7a7a\u767d\u5b57\u7b26\u3002\u4e0b\u9762\u7684\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86trim\u662f\u600e\u6837\u5904\u7406\u5b57\u7b26\u4e32\u91cc\u9762\u7684\u6362\u884c\u7b26\u7684\uff1a<\/p>\n<p><pre lang=\"javascript\">\nvar trimDemo = function(){\n    \/\/ \u8bbe\u7f6e\u6211\u4eec\u8981\u4fee\u526a\u7684\u5b57\u7b26\u4e32\n    var text_to_trim =  '            \\ntoo       much       whitespace\\n              ';\n \n    \/\/ \u5bf9\u5176\u8fdb\u884c\u4fee\u526a\n    var trimmed_text = text_to_trim.trim();\n \n    \/\/ \u663e\u793a\u7ed3\u679c\n    alert('Before Trimming : \\n' + \n          '|-' + text_to_trim + '-|\\n\\n' +\n          'After Trimming : \\n' +  \n          '|-' + trimmed_text + '-|');\n}\n<\/pre>\n<\/p>\n<p>\n<script type=\"text\/javascript\">\nvar trimDemo = function(){\n    var text_to_trim =  '            \\ntoo       much       whitespace\\n              ';\n    var trimmed_text = text_to_trim.trim();\n    alert('Before Trimming : \\n' + \n          '|-' + text_to_trim + '-|\\n\\n' +\n          'After Trimming : \\n' +  \n          '|-' + trimmed_text + '-|');\n}\n<\/script><br \/>\n<input type=\"button\" value=\"Trim\" onclick=\"trimDemo()\" \/>\n<\/p>\n<h3>clean()<\/h3>\n<p>\u4e3a\u4e86\u66f4\u6709\u610f\u4e49\uff0c\u4f60\u4e5f\u8bb8\u4e0d\u9700\u8981\u53bb\u6389\u4e00\u4e2a\u5b57\u7b26\u4e32\u91cc\u7684\u6240\u6709\u7a7a\u767d\u7b26\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5bf9\u4e8e\u90a3\u4e9b\u4f60\u89c9\u5f97\u575a\u5f3a\u7684\u7a7a\u767d\u5b57\u7b26\uff0cMooTools\u6177\u6168\u5730\u4e3a\u4f60\u63d0\u4f9b\u4e86clean()\u65b9\u6cd5\u3002<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u8fd9\u662f\u6211\u4eec\u8981\u4fee\u526a\u7684\u5b57\u7b26\u4e32\nvar text_to_clean =  &quot;    \\nString     \\nWith    Lots \\n \\n    More     \\nWhitespace  \\n   &quot;;\n\/\/ clean\u4ee5\u540e\u7684\u5b57\u7b26\u4e32\u662f&quot;String With Lots More Whitespace&quot;\nvar cleaned_text  = text_to_trim.clean();\n<\/pre>\n<\/p>\n<p>clean()\u65b9\u6cd5\u4e0etrim()\u65b9\u6cd5\u6709\u4e00\u70b9\u5f88\u5927\u7684\u4e0d\u540c\u3002\u5b83\u628a\u4f60\u4f20\u5165\u7684\u5b57\u7b26\u91cc\u9762\u7684\u7a7a\u683c\u5168\u90e8\u63d0\u53d6\u51fa\u6765\uff0c\u800c\u4e0d\u53ea\u662f\u5934\u90e8\u548c\u5c3e\u90e8\u7684\u7a7a\u767d\u5b57\u7b26\u3002\u5b83\u4eec\u610f\u5473\u7740\u5b57\u7b26\u4e32\u4e2d\u7684\u4efb\u4f55\u591a\u4e8e\u4e00\u4e2a\u7684\u7a7a\u767d\u5b57\u7b26\u548c\u4efb\u4f55\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff08tab\uff09\u3002\u5bf9\u6bd4\u4e00\u4e0b\u4fee\u526a\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u770b\u770b\u5230\u5e95\u662f\u4ec0\u4e48\u610f\u601d\uff1a<\/p>\n<p><pre lang=\"javascript\">\nvar cleanDemo = function(){\n    \/\/ \u8bbe\u7f6e\u6211\u4eec\u8981\u4fee\u526a\u7684\u5b57\u7b26\u4e32\n    var text_to_clean =  '            too\\n       much\\n       whitespace              ';\n \n    \/\/ clean\u8be5\u5b57\u7b26\u4e32\n    var cleaned_text = text_to_clean.clean();\n \n    \/\/ \u663e\u793a\u7ed3\u679c\n    alert('Before Cleaning : \\n' + \n          '|-' + text_to_clean + '-|\\n\\n' +\n          'After Cleaning : \\n' +  \n          '|-' + cleaned_text + '-|');\n}\n<\/pre>\n<\/p>\n<p>\n<script type=\"text\/javascript\">\nvar cleanDemo = function(){\n    var text_to_clean =  '            too\\n       much\\n       whitespace              ';\n    var cleaned_text = text_to_clean.clean();\n    alert('Before Cleaning : \\n' + \n          '|-' + text_to_clean + '-|\\n\\n' +\n          'After Cleaning : \\n' +  \n          '|-' + cleaned_text + '-|');\n}\n<\/script><br \/>\n<input type=\"button\" value=\"Clean\" onclick=\"cleanDemo()\" \/>\n<\/p>\n<h3>contains()<\/h3>\n<p>\u548ctrim()\u4ee5\u53caclean()\u65b9\u6cd5\u7c7b\u4f3c\uff0ccontains()\u65b9\u6cd5\u505a\u4e00\u4ef6\u5f88\u7b80\u5355\u7684\u4e8b\u60c5\uff0c\u6ca1\u6709\u4efb\u4f55\u5176\u4ed6\u7684\u82b1\u67b6\u5b50\u3002\u5b83\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u53bb\u770b\u5b83\u662f\u5426\u5305\u542b\u4e00\u4e2a\u4f60\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u627e\u5230\u4e86\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u5c31\u8fd4\u56detrue\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5c31\u8fd4\u56defalse\u3002<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u6211\u4eec\u8981\u5728\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u9762\u67e5\u627e\nvar string_to_match = &quot;Does this contain thing work?&quot;;\n \n\/\/ \u627e'contain', did_string match\u4e3atrue\nvar did_string_match = string_to_match.contains('contain');\n \n\/\/ \u627e'propane', did_string_match\u4e3a false\ndid_string_match = string_to_match.contains('propane');\n<\/pre>\n<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u6d3e\u4e0a\u7528\u573a\uff0c\u5f53\u4f60\u548c\u5176\u4ed6\u5de5\u5177\uff0c\u5982\u6211\u4eec\u5728\u7b2c\u4e09\u8bb2\u4e2d\u8bb2\u5230\u7684Array.each()\u51fd\u6570\u914d\u5408\u4f7f\u7528\u65f6\uff0c\u4f60\u53ef\u4ee5\u7528\u76f8\u5bf9\u8f83\u5c11\u7684\u4ee3\u7801\u6765\u5b8c\u6210\u4e00\u4e9b\u7a0d\u5fae\u590d\u6742\u7684\u4efb\u52a1\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u628a\u4e00\u7cfb\u5217\u5355\u8bcd\u653e\u8fdb\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u4e00\u4e2a\u4e00\u4e2a\u5730\u904d\u5386\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u8f83\u5c11\u7684\u4ee3\u7801\u5728\u4e00\u4e2a\u6587\u672c\u7684\u76f8\u540c\u533a\u57df\u4e2d\u5bfb\u627e\u591a\u4e2a\u5355\u8bcd\uff1a<\/p>\n<p><pre lang=\"javascript\">\nstring_to_match = &quot;string containing whatever words you want to try to match&quot;;\n    word_array = ['words', 'to', 'match'];\n    \/\/ \u628a\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5355\u8bcd\u4f5c\u4e3a\u53d8\u91cf\u4f20\u8fdb\u53bb\n    word_array.each(function(word_to_match){\n        \/\/ \u5bfb\u627e\u5f53\u524d\u7684\u5355\u8bcd\n        if (string_to_match.contains(word_to_match)){\n            alert('found ' + word_to_match);\n        };\n    });\n<\/pre>\n<\/p>\n<p>\u6211\u4eec\u628a\u5b83\u653e\u8fdb\u4e00\u4e2atextbox\u4e2d\uff0c\u52a0\u4e00\u70b9\u60f3\u8c61\uff0c\u4f60\u5c31\u53ef\u4ee5\u62e5\u6709\u4f60\u81ea\u5df1\u7684\u810f\u8bcd\uff08\u6216\u8005\u5176\u4ed6\u4efb\u4f55\uff09\u68c0\u6d4b\u5668\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar containsDemo = function(){\n    \/\/ \u628a\u6211\u4eec\u8981\u7981\u6b62\u7684\u8bcd\u653e\u8fdb\u4e00\u4e2a\u6570\u7ec4\n    var banned_words = ['drat', 'goshdarn', 'fiddlesticks', 'kumquat'];    \n \n    \/\/ \u83b7\u5f97\u6587\u672c\u57df\u4e2d\u7684\u5185\u5bb9\n    var textarea_input = $('textarea_1').get('value');\n \n    \/\/ \u679a\u4e3e\u8fc7\u6ee4\u8bcd\u4e2d\u7684\u6bcf\u4e00\u4e2a\u8bcd\n    banned_words.each(function(banned_word){\n        \/\/ \u5728\u6587\u672c\u57df\u5185\u5bb9\u4e2d\u67e5\u627e\u5f53\u524d\u7684\u8fc7\u6ee4\u8bcd\n        if (textarea_input.contains(banned_word)){\n            \/\/ \u544a\u8bc9\u7528\u6237\u4e0d\u80fd\u4f7f\u7528\u90a3\u4e2a\u5355\u8bcd\n            alert(banned_word + ' is not allowed');\n        };\n    });\n}\n<\/pre>\n<\/p>\n<p>\n<script type=\"text\/javascript\">\nvar containsDemo = function(){\n    var banned_words = ['drat', 'goshdarn', 'fiddlesticks', 'kumquat']; \n    var textarea_input = $('textarea_1').get('value');\n    banned_words.each(function(banned_word){\n        if (textarea_input.contains(banned_word)){\n            alert(banned_word + ' is not allowed');\n        };\n    });\n}\n<\/script><\/p>\n<style type=\"text\/css\">\ninput.t_white{background:none;background:#fff;color:#333}\n<\/style>\n<p><input type=\"text\" id=\"textarea_1\" class=\"t_white\" size=\"60\" value=\"Banned Words : drat goshdarn fiddlesticks kumquat\" \/><br \/>\n<input type=\"button\" value=\"Check for Banned Words\" onclick=\"containsDemo()\" \/>\n<\/p>\n<h3>substitute()<\/h3>\n<p>substitute()\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u6211\u4eec\u4eca\u5929\u53ea\u662f\u8bb2\u4e00\u4e0b\u4e00\u4e9b\u5173\u4e8e\u5b83\u7684\u57fa\u672c\u77e5\u8bc6\uff0csubstitute\u7684\u66f4\u591a\u5f3a\u5927\u7684\u529f\u80fd\u6765\u81ea\u4e8e\u5b83\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7a0d\u5fae\u8bb2\u4e00\u4e0b\u3002\u7136\u800c\uff0c\u4ec5\u4ec5\u4f7f\u7528\u8fd9\u4e9b\u57fa\u672c\u529f\u80fd\u4f60\u5c31\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b\u60c5\u4e86\u3002<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u8fd9\u662f\u8981\u4f7f\u7528substitute\u65b9\u6cd5\u7684\u6587\u672c\u6a21\u677f\n\/\/ \u6ce8\u610f\uff0c\u8981\u66ff\u4ee3\u7684\u90e8\u5206\u90fd\u662f\u7528\u82b1\u62ec\u53f7\u62ec\u8d77\u6765\u7684\u90e8\u5206\nvar text_for_substitute = &quot;One is {one},  Two {two}, Three is {three}.&quot;;\n \n\/\/ \u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u4e86\u8981\u66ff\u6362\u7684\u89c4\u5219\n\/\/ \u6ca1\u6709\u7528\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u90e8\u5206\u662f\u641c\u7d22\u9879\n\/\/ \u7528\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u90e8\u5206\u662f\u7528\u6765\u66ff\u6362\u641c\u7d22\u9879\u7684\u53e5\u5b50\nvar substitution_object = {\n    one   : 'the first variable', \n    two   : 'always comes second', \n    three : 'getting sick of bronze..'\n    };\n \n\/\/ \u5728text_for_substitute\u4e0a\u8c03\u7528substitute\u65b9\u6cd5\n\/\/ \u628asubstitution_object\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\n\/\/ \u628a\u66ff\u6362\u7ed3\u679c\u8d4b\u503c\u7ed9\u53d8\u91cfnew_string\nvar new_string = text_for_substitute.substitute(substitution_object); \n \n\/\/ new_string\u73b0\u5728\u7684\u503c\u4e3a&quot;One is the first variable,  Two always comes second, Three is getting sick of bronze...&quot;\n<\/pre>\n<\/p>\n<p>\u4e8b\u5b9e\u4e0a\u4f60\u5e76\u4e0d\u9700\u8981\u521b\u5efa\u4e00\u4e2asubstitution_object\u5bf9\u8c61\u6765\u4f7f\u7528substitute\u65b9\u6cd5\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u5b83\u4e0d\u5408\u9002\u7684\u8bdd\uff0c\u4e0b\u9762\u7684\u65b9\u6cd5\u4e5f\u540c\u6837\u53ef\u4ee5\u5b9e\u73b0\uff1a<\/p>\n<p><pre lang=\"javascript\">\n\/\/ \u5efa\u7acb\u8981\u66ff\u6362\u7684\u5b57\u7b26\u4e32\nvar text_for_substitute = &quot;{substitute_key} and the original text&quot;;\n\/\/ \u628a\u8981\u66ff\u6362\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9substitute\u65b9\u6cd5\nvar result_text = text_for_substitute.substitute({substitute_key : 'substitute_value'});\n\/\/ result_text\u73b0\u5728\u5c31\u662f&quot;substitute_value and the original text&quot;\n<\/pre>\n<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u505a\u5f97\u66f4\u591a\u66f4\u6df1\u5165\u4e00\u70b9\uff0c\u4f60\u53ef\u4ee5\u7528\u4ece\u4e00\u4e2aDOM\u5bf9\u8c61\u4e2d\u83b7\u5f97\u503c\u7684\u51fd\u6570\u8c03\u7528\u6765\u4f5c\u4e3a\u66ff\u6362\u9879\u7684\u503c\uff0c\u8fd9\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<p><pre lang=\"javascript\">\nvar substituteDemo = function(){\n    \/\/ \u4ecetextfield\u4e2d\u83b7\u5f97\u539f\u59cb\u7684 \u6587\u672c\n    var original_text = $('substitute_span').get('html');\n \n    \/\/ \u7528\u6587\u672c\u6846\u4e2d\u7684\u503c\u66ff\u6362textfield\u4e2d\u7684\u503c\n    var new_text = original_text.substitute({\n        first  : $('first_value').get('value'),\n        second : $('second_value').get('value'),\n        third  : $('third_value').get('value'),\n    });\n \n    \/\/ \u7528\u65b0\u7684\u6587\u672c\u66ff\u6362span\u4e2d\u7684\u5185\u5bb9\n    $('substitute_span').set('html', new_text);\n \n \n    \/\/ \u7981\u7528substitute\u6309\u94ae\n    \/\/ \u5e76\u542f\u7528reset\u6309\u94ae\n    $('simple_substitute').set('disabled', true);\n    $('simple_sub_reset').set('disabled', false);\n}\n \nvar substituteReset = function(){\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u539f\u6709\u7684\u6587\u672c\n    var original_text = &quot;|- {first} -- {second} -- {third} -|&quot;;\n \n    \/\/ \u7528\u539f\u6709\u7684\u6587\u672c\u6765\u66ff\u6362span\u4e2d\u7684\u5185\u5bb9\n    $('substitute_span').set('html', original_text);\n \n    \/\/ \u7981\u7528reset\u6309\u94ae\n    \/\/ \u5e76\u542f\u7528substitute\n    $('simple_sub_reset').set('disabled', true);\n    $('simple_substitute').set('disabled', false);\n}\n<\/pre>\n<\/p>\n<p>\n<script type=\"text\/javascript\">\nvar substituteDemo = function(){\n\tvar original_text = $('substitute_span').get('html');\n\tvar new_text = original_text.substitute({\n\t\tfirst  : $('first_value').get('value'),\n\t\tsecond : $('second_value').get('value'),\n\t\tthird  : $('third_value').get('value'),\n\t});<\/p>\n<p>\t$('substitute_span').set('html', new_text);\n\t$('simple_substitute').set('disabled', true);\n\t$('simple_sub_reset').set('disabled', false);\n}<\/p>\n<p>var substituteReset = function(){\n\tvar original_text = \"|- {first} -- {second} -- {third} -|\";\n\t$('substitute_span').set('html', original_text);\n\t$('simple_sub_reset').set('disabled', true);\n\t$('simple_substitute').set('disabled', false);\n}\n<\/script><br \/>\n<br \/><span id=\"substitute_span\">|- {first} &#8212; {second} &#8212; {third} -|<\/span><br \/>\nfirst_value<br \/>\n<input id=\"first_value\" class=\"t_white\" type=\"text\" \/><\/p>\n<p>second_value<br \/>\n<input id=\"second_value\" class=\"t_white\" type=\"text\" \/><\/p>\n<p>third_value<br \/>\n<input id=\"third_value\" class=\"t_white\" type=\"text\" \/><\/p>\n<p><input id=\"simple_substitute\" type=\"button\" onclick=\"substituteDemo()\" value=\"substituteDemo()\" \/>\u00a0\u00a0<br \/>\n<input id=\"simple_sub_reset\" type=\"button\" onclick=\"substituteReset()\" value=\"substituteReset()\" \/>\n<\/p>\n<p>\u5728\u4eca\u5929\u7ed3\u675f\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u5f88\u5c0f\u7684\u63d0\u793a\uff0c\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e0a\u8c03\u7528substitute\u65b9\u6cd5\uff0c\u5e76\u4e14\u4e0d\u4e3a\u8981\u66ff\u6362\u7684\u5173\u952e\u5b57\u63d0\u4f9b\u4e00\u4e2a\u952e\/\u503c\u5bf9\uff08key\/value pair\uff09\u5bf9\u8c61\uff0c\u90a3\u4e48\u5b83\u5c06\u53ea\u662f\u7b80\u5355\u5730\u5220\u9664\u6389\u82b1\u62ec\u53f7\u91cc\u9762\u7684\u5185\u5bb9\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9700\u8981\u4fdd\u7559\u82b1\u62ec\u53f7\u91cc\u9762\u7684\u5b57\u7b26\u4e32\uff0c\u8bf7\u6ce8\u610f\u4e0d\u8981\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><pre lang=\"javascript\">\n(&quot;{one} some stuff {two} some more stuff&quot;).substitute({one : 'substitution text'});\n<\/pre>\n<\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u201csubstitution text some stuff some more stuff\u201d\u3002<\/p>\n<h3>\u66f4\u591a\u5b66\u4e60<\/h3>\n<p><b><a href=\"http:\/\/www.consideropen.com\/downloads\/30days_of_moo\/mootorial_day9_filtering_2.zip\" target=\"_blank\" title=\"\u4e0b\u8f7d\u4e00\u4e2a\u5305\u542b\u4f60\u5f00\u59cb\u6240\u9700\u8981\u7684zip\u5305\">\u4e0b\u8f7d\u4e00\u4e2a\u5305\u542b\u4f60\u5f00\u59cb\u6240\u9700\u8981\u7684zip\u5305<\/a><\/b><\/p>\n<ul style=\"list-style-type:disc\">\n<li><a href=\"http:\/\/www.quirksmode.org\/js\/strings.html\" target=\"_blank\" title=\"String\u4e0a\u7684\u602a\u5f02\u6a21\u5f0f\">String\u4e0a\u7684\u602a\u5f02\u6a21\u5f0f<\/a>\uff08this guy is amazing\uff09<\/li>\n<li><a href=\"http:\/\/www.w3schools.com\/jsref\/jsref_obj_global.asp\" target=\"_blank\" title=\"JavaScript\u5b57\u7b26\u4e32\u51fd\u6570\u53c2\u8003\">JavaScript\u5b57\u7b26\u4e32\u51fd\u6570\u53c2\u8003<\/a><\/li>\n<li><a href=\"http:\/\/docs.mootools.net\/Native\/String\" target=\"_blank\" title=\"MooTools\u5b57\u7b26\u4e32\u6587\u6863\">MooTools\u5b57\u7b26\u4e32\u6587\u6863<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1a30 Days of Mootools 1.2 Tutorials &#8211; Day 9 &#8211; Input Filtering Part II (Strings) \u8f93\u5165\u8fc7\u6ee4\u7b2c\u4e8c\u90e8\u5206\uff08\u5b57\u7b26\u4e32\uff09 \u8bf7\u5c0a\u91cd\u4e2a\u4eba\u52b3\u52a8\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttp:\/\/fdream.net\uff0c \u8bd1\u8005\uff1aFdream \u5b57\u7b26\u4e32\u51fd\u6570 \u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u51c6\u5907\u597d\u5f00\u59cb\u8fd9\u4e00\u8bb2\uff0c\u8bf7\u53c2\u8003\u8fd9\u4e00\u7cfb\u5217\u7684\u6559\u7a0b\uff0c\u8fd9\u91cc\u662f\u300aMooTools 1.2\u7cfb\u5217\u6559\u7a0b\u76ee\u5f55\u300b\u3002 \u4eca\u5929\u6211\u4eec\u6765\u770b\u4e00\u770bMooTools\u7ed9\u6211\u4eec\u63d0\u4f9b\u7684\u989d\u5916\u7684\u4e00\u4e9b\u5904\u7406\u5b57\u7b26\u51fd\u6570\u3002\u8fd9\u53ea\u662fMooTools\u5b57\u7b26\u4e32\u5904\u7406\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u4e0d\u5305\u542b\u4e00\u4e9b\u795e\u79d8\u7684\u51fd\u6570\uff08\u6bd4\u5982toCamelCase()\uff09\u548c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u3002\u6211\u4eec\u4f1a\u5728\u4ee5\u540e\u53e6\u5916\u7528\u4e00\u8bb2\u6765\u5c06\u4e00\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u77e5\u8bc6\u548c\u5728MooTools\u4e0b\u7684\u4f7f\u7528\u3002 \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u60f3\u5148\u82b1\u4e00\u70b9\u65f6\u95f4\u6765\u770b\u4e00\u4e0b\u5b57\u7b26\u4e32\u51fd\u6570\u662f\u600e\u4e48\u8c03\u7528\u7684\u3002\u5728\u6211\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u662f\u5728\u5b57\u7b26\u4e32\u53d8\u91cf\u4e0a\u9762\u76f4\u63a5\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\uff0c\u5c31\u50cf\u4e0b\u9762\u7684\u8fd9\u6837\uff1a var my_text_variable = &quot;Heres some text&quot;; \/\/ \u7ed3\u679c \u5b57\u7b26\u4e32\u53d8\u91cf \u65b9\u6cd5\u540d var result_of_function = my_text_variable.someStringFunction(); \u4f46\u662f\u6211\u8fd9\u6837\u5199\u53ea\u662f\u4e3a\u4e86\u80fd\u591f\u66f4\u6e05\u695a\u5730\u89e3\u91ca\u5b83\uff0c\u4f60\u5e94\u8be5\u4e86\u89e3\u5230\u8fd9\u4e9b\u5b57\u7b26\u4e32\u51fd\u6570\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e0a\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a var result_of_function = &quot;Heres some text&quot;.someStringFunction(); \u6ce8\u610f\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u65b9\u5f0f\u5728MooTools\u4e2d\u7684\u6570\u5b57\u5904\u7406\u51fd\u6570\u4e5f\u540c\u6837\u6709\u6548\uff1a \/\/ \u6ce8\u610f\u4e00\u4e0b\u7528\u6cd5\uff0c\u662f\u62ec\u53f7\u4e2d\u7684\u6570\u5b57 \/\/ \u800c\u4e0d\u662f\u5355\u5f15\u53f7\u5f15\u8d77\u6765\u7684\u5b57\u7b26\u4e32 var limited_number = (256).limit(1, 100); \u8fd8\u6709\uff0c\u6211\u60f3\u518d\u6b21\u5f3a\u8c03\u4e00\u904d\uff1a\u7528JavaScript\u5bf9\u8f93\u5165\u8fc7\u6ee4\u5e76\u4e0d\u80fd\u5728\u6570\u636e\u53d1\u9001\u5230\u670d\u52a1\u5668\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u5b89\u5168\u8fc7\u6ee4\u3002\u4f60\u5728JavaScript\u4e2d\u5199\u7684\u6240\u6709\u7684\u4e00\u5207\u90fd\u53ef\u4ee5\u88ab\u4f60\u7684\u7f51\u9875\u6d4f\u89c8\u8005\u770b\u5230\u3001\u64cd\u63a7\u548c\u7981\u6b62\u3002\u6211\u4eec\u5c06\u5728\u4ee5\u540e\u8bb2MooTools\u7684Request\u7c7b\u65f6\uff0c\u5bf9PHP\u7684\u8fc7\u6ee4\u6280\u672f\u8fdb\u884c\u4e00\u4e9b\u7b80\u5355\u7684\u63a2\u8ba8\u3002\u540c\u65f6\uff0c\u7ee7\u7eed\u4fdd\u6301\u539f\u6765\u8981\u5728\u670d\u52a1\u5668\u7aef\u505a\u7684\u4efb\u4f55\u4e0e\u5b89\u5168\u76f8\u5173\u7684\u4e8b\u60c5\uff0c\u4e0d\u8981\u4f9d\u8d56JavaScript\u3002 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[120,104,328,123],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-coding","tag-AJAX","tag-JavaScript","tag-mootools","tag-123"],"views":9355,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}