Editing Build a regexp table This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) The fastest way to build a replacing function based on regexps. See replacer in https://github.com/NaturalNode/natural/blob/master/lib/natural/util/utils.js This checks only the building of replacing functions, not its actual use. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script> var table = {"㋀":"1月","㋁":"2月","㋂":"3月","㋃":"4月","㋄":"5月","㋅":"6月","㋆":"7月","㋇":"8月","㋈":"9月","㋉":"10月","㋊":"11月","㋋":"12月","㏠":"1日","㏡":"2日","㏢":"3日","㏣":"4日","㏤":"5日","㏥":"6日","㏦":"7日","㏧":"8日","㏨":"9日","㏩":"10日","㏪":"11日","㏫":"12日","㏬":"13日","㏭":"14日","㏮":"15日","㏯":"16日","㏰":"17日","㏱":"18日","㏲":"19日","㏳":"20日","㏴":"21日","㏵":"22日","㏶":"23日","㏷":"24日","㏸":"25日","㏹":"26日","㏺":"27日","㏻":"28日","㏼":"29日","㏽":"30日","㏾":"31日","㍘":"0点","㍙":"1点","㍚":"2点","㍛":"3点","㍜":"4点","㍝":"5点","㍞":"6点","㍟":"7点","㍠":"8点","㍡":"9点","㍢":"10点","㍣":"11点","㍤":"12点","㍥":"13点","㍦":"14点","㍧":"15点","㍨":"16点","㍩":"17点","㍪":"18点","㍫":"19点","㍬":"20点","㍭":"21点","㍮":"22点","㍯":"23点","㍰":"24点","㍻":"平成","㍼":"昭和","㍽":"大正","㍾":"明治","㍿":"株式会社","㌀":"アパート","㌁":"アルファ","㌂":"アンペア","㌃":"アール","㌄":"イニング","㌅":"インチ","㌆":"ウオン","㌇":"エスクード","㌈":"エーカー","㌉":"オンス","㌊":"オーム","㌋":"カイリ","㌌":"カラット","㌍":"カロリー","㌎":"ガロン","㌏":"ガンマ","㌐":"ギガ","㌑":"ギニー","㌒":"キュリー","㌓":"ギルダー","㌔":"キロ","㌕":"キログラム","㌖":"キロメートル","㌗":"キロワット","㌘":"グラム","㌙":"グラムトン","㌚":"クルゼイロ","㌛":"クローネ","㌜":"ケース","㌝":"コルナ","㌞":"コーポ","㌟":"サイクル","㌠":"サンチーム","㌡":"シリング","㌢":"センチ","㌣":"セント","㌤":"ダース","㌥":"デシ","㌦":"ドル","㌧":"トン","㌨":"ナノ","㌩":"ノット","㌪":"ハイツ","㌫":"パーセント","㌬":"パーツ","㌭":"バーレル","㌮":"ピアストル","㌯":"ピクル","㌰":"ピコ","㌱":"ビル","㌲":"ファラッド","㌳":"フィート","㌴":"ブッシェル","㌵":"フラン","㌶":"ヘクタール","㌷":"ペソ","㌸":"ペニヒ","㌹":"ヘルツ","㌺":"ペンス","㌻":"ページ","㌼":"ベータ","㌽":"ポイント","㌾":"ボルト","㌿":"ホン","㍀":"ポンド","㍁":"ホール","㍂":"ホーン","㍃":"マイクロ","㍄":"マイル","㍅":"マッハ","㍆":"マルク","㍇":"マンション","㍈":"ミクロン","㍉":"ミリ","㍊":"ミリバール","㍋":"メガ","㍌":"メガトン","㍍":"メートル","㍎":"ヤード","㍏":"ヤール","㍐":"ユアン","㍑":"リットル","㍒":"リラ","㍓":"ルピー","㍔":"ルーブル","㍕":"レム","㍖":"レントゲン","㍗":"ワット"}; function replacer1(translationTable) { /** * An array of translationTable keys. * @type {Array.<string>} */ var pattern = []; /** * The regular expression doing the replacement job. * @type {RegExp} */ var regExp; /** * Used to iterate over translationTable. * @type {string} */ var key; for (key in translationTable) { // Escaping regexp special chars. key = key.replace(/(\^|\$|\*|\+|\?|\.|\(|\)|\[|\]|\{|\}|\||\\|\/)/g, '\\\$1'); pattern.push(key); } regExp = new RegExp(pattern.join('|'), 'g'); /** * @param {string} str Input string. * @return {string} The string replaced. */ return function(str) { return str.replace(regExp, function(str) { return translationTable[str]; }); }; } function replacer2(translationTable) { /** * The regular expression doing the replacement job. * @type {RegExp} */ var regExp; /** * An array containing the keys of the replacement table. * @type {Array.<string>} */ var keys = Object.keys(translationTable); keys = keys.map(function(key) { return key.replace(/(\^|\$|\*|\+|\?|\.|\(|\)|\[|\]|\{|\}|\||\\|\/)/g, '\\\$1'); }); regExp = new RegExp(keys.join('|'), 'g'); /** * @param {string} str Input string. * @return {string} The string replaced. */ return function(str) { return str.replace(regExp, function(str) { return translationTable[str]; }); }; } function replacer3(translationTable) { /** * The regular expression doing the replacement job. * @type {RegExp} */ var regExp; /** * An array containing the keys of the replacement table. * @type {Array.<string>} */ var keys = Object.keys(translationTable); for (var i = 0, len = keys.length; i < len; i++) { keys[i] = keys[i].replace(/(\^|\$|\*|\+|\?|\.|\(|\)|\[|\]|\{|\}|\||\\|\/)/g, '\\\$1'); } regExp = new RegExp(keys.join('|'), 'g'); /** * @param {string} str Input string. * @return {string} The string replaced. */ return function(str) { return str.replace(regExp, function(str) { return translationTable[str]; }); }; } </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code replacer1(table); Test 2 Title Async (check if this is an asynchronous test) Code replacer2(table); Test 3 Title Async (check if this is an asynchronous test) Code replacer3(table);