From 8e6d484adab430e9740118dd34fceda9951cb8d0 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Mon, 24 Mar 2014 12:03:05 +0000 Subject: [PATCH] Escape regex special characters in tag finder refs #2149 - Properly highlight tags with special characters ($,[,],^,etc.) --- ghost/admin/views/editor-tag-widget.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ghost/admin/views/editor-tag-widget.js b/ghost/admin/views/editor-tag-widget.js index 901cf3b7f0..7dc4cc40a5 100644 --- a/ghost/admin/views/editor-tag-widget.js +++ b/ghost/admin/views/editor-tag-widget.js @@ -106,8 +106,11 @@ styles = { left: $target.position().left }, - maxSuggestions = 5, // Limit the suggestions number - regexTerm = searchTerm.replace(/(\s+)/g, "(<[^>]+>)*$1(<[^>]+>)*"), + // Limit the suggestions number + maxSuggestions = 5, + // Escape regex special characters + escapedTerm = searchTerm.replace(/[\-\/\\\^$*+?.()|\[\]{}]/g, '\\$&'), + regexTerm = escapedTerm.replace(/(\s+)/g, "(<[^>]+>)*$1(<[^>]+>)*"), regexPattern = new RegExp("(" + regexTerm + ")", "i"); this.$suggestions.css(styles); @@ -120,6 +123,7 @@ _.each(matchingTags, function (matchingTag) { var highlightedName, suggestionHTML; + highlightedName = matchingTag.name.replace(regexPattern, function (match, p1) { return "" + _.escape(p1) + ""; });