From 853794a4fd006b57904ad8595bc6eb09eb507f2d Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 31 Jul 2012 16:15:16 +0200 Subject: [PATCH] Make at least forward regexp search behave properly wrt $ and ^ --- lib/util/searchcursor.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/util/searchcursor.js b/lib/util/searchcursor.js index 7e0f0ebfcf..970af899d7 100644 --- a/lib/util/searchcursor.js +++ b/lib/util/searchcursor.js @@ -10,29 +10,32 @@ // It takes a position and a direction, and returns an object // describing the next occurrence of the query, or null if no // more matches were found. - if (typeof query != "string") // Regexp match + if (typeof query != "string") { // Regexp match + if (!query.global) query = new RegExp(query.source, query.ignoreCase ? "ig" : "g"); this.matches = function(reverse, pos) { if (reverse) { - var line = cm.getLine(pos.line).slice(0, pos.ch), match = line.match(query), start = 0; + query.lastIndex = 0; + var line = cm.getLine(pos.line).slice(0, pos.ch), match = query.exec(line), start = 0; while (match) { start += match.index; line = line.slice(match.index); - var newmatch = line.match(query); + query.lastIndex = 0; + var newmatch = query.exec(line); if (newmatch) match = newmatch; else break; start++; } - } - else { - var line = cm.getLine(pos.line).slice(pos.ch), match = line.match(query), - start = match && pos.ch + match.index; + } else { + query.lastIndex = pos.ch; + var line = cm.getLine(pos.line), match = query.exec(line), + start = match && match.index; } if (match) return {from: {line: pos.line, ch: start}, to: {line: pos.line, ch: start + match[0].length}, match: match}; }; - else { // String query + } else { // String query if (caseFold) query = query.toLowerCase(); var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;}; var target = query.split("\n");