From 40fef4e3608feb3bf2ec89dfbcd643a307096948 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 7 May 2015 13:23:53 +0000 Subject: [PATCH] Fix "replace" Handlebars.js helper --- .../js/source/default/handlebars_helpers.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/mibew/js/source/default/handlebars_helpers.js b/src/mibew/js/source/default/handlebars_helpers.js index 20edff31..063f0f26 100644 --- a/src/mibew/js/source/default/handlebars_helpers.js +++ b/src/mibew/js/source/default/handlebars_helpers.js @@ -193,14 +193,17 @@ * */ Handlebars.registerHelper('replace', function(search, replacement, options) { - // Convert serch value to string and escape special regexp characters - var searchPattern = search.toString().replace( - /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, - "\\$&" - ), - re = new RegExp(searchPattern, 'g'); + var unescapedSearch = search + // Allow using new line character + .replace(/\\n/g, '\n') + // Allow using tab character + .replace(/\\t/g, '\t') + // Allow using all UTF characters in \uXXX format. + .replace(/\\u([A-Za-z0-9])/g, function(match, code) { + return String.fromCharCode(parseInt(code, 16)); + }); - return options.fn(this).replace(re, replacement); + return options.fn(this).split(unescapedSearch).join(replacement); }); /**