0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Escape regex special characters in tag finder

refs #2149
- Properly highlight tags with special characters ($,[,],^,etc.)
This commit is contained in:
Fabian Becker 2014-03-24 12:03:05 +00:00
parent ae3c36797a
commit 9ba80ba359

View file

@ -106,8 +106,11 @@
styles = { styles = {
left: $target.position().left left: $target.position().left
}, },
maxSuggestions = 5, // Limit the suggestions number // Limit the suggestions number
regexTerm = searchTerm.replace(/(\s+)/g, "(<[^>]+>)*$1(<[^>]+>)*"), maxSuggestions = 5,
// Escape regex special characters
escapedTerm = searchTerm.replace(/[\-\/\\\^$*+?.()|\[\]{}]/g, '\\$&'),
regexTerm = escapedTerm.replace(/(\s+)/g, "(<[^>]+>)*$1(<[^>]+>)*"),
regexPattern = new RegExp("(" + regexTerm + ")", "i"); regexPattern = new RegExp("(" + regexTerm + ")", "i");
this.$suggestions.css(styles); this.$suggestions.css(styles);
@ -120,6 +123,7 @@
_.each(matchingTags, function (matchingTag) { _.each(matchingTags, function (matchingTag) {
var highlightedName, var highlightedName,
suggestionHTML; suggestionHTML;
highlightedName = matchingTag.name.replace(regexPattern, function (match, p1) { highlightedName = matchingTag.name.replace(regexPattern, function (match, p1) {
return "<mark>" + _.escape(p1) + "</mark>"; return "<mark>" + _.escape(p1) + "</mark>";
}); });