@@ -274,22 +274,37 @@ function parseString(string) {
274
274
} ) ;
275
275
}
276
276
277
- function parseQuery ( query ) {
278
- var isRE = query . match ( / ^ \/ ( .* ) \/ ( [ a - z ] * ) $ / ) ;
279
- if ( isRE ) {
280
- try {
281
- query = new RegExp ( isRE [ 1 ] , isRE [ 2 ] . indexOf ( 'i' ) == - 1 ? '' : 'i' ) ;
282
- } catch ( e ) { } // Not a regular expression after all, do a string search
277
+ function parseQuery ( query , state ) {
278
+ var emptyQuery = 'x^' ; // matches nothing
279
+ if ( query === '' ) { // empty string matches nothing
280
+ query = emptyQuery ;
283
281
} else {
284
- query = parseString ( query ) ;
282
+ if ( state . regexp === false ) {
283
+ query = parseString ( query ) ;
284
+ query = query . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, "\\$&" ) ;
285
+ }
286
+ if ( state . wholeWord ) {
287
+ query += '\\b' ;
288
+ }
289
+ }
290
+
291
+ var regexp ;
292
+ try {
293
+ regexp = new RegExp ( query , state . caseInsensitive ? "gi" : "g" ) ;
294
+ } catch ( e ) {
295
+ regexp = new RegExp ( emptyQuery , 'g' ) ;
296
+ }
297
+ // If the resulting regexp will match everything, do not use it
298
+ if ( regexp . test ( '' ) ) {
299
+ return new RegExp ( emptyQuery , 'g' ) ;
285
300
}
286
- if ( typeof query == 'string' ? query == '' : query . test ( '' ) ) query = / x ^ / ;
287
- return query ;
301
+ return regexp ;
288
302
}
289
303
290
304
function startSearch ( cm , state , query ) {
291
305
state . queryText = query ;
292
- state . query = parseQuery ( query ) ;
306
+ state . lastQuery = query ;
307
+ state . query = parseQuery ( query , state ) ;
293
308
cm . removeOverlay ( state . overlay , state . caseInsensitive ) ;
294
309
state . overlay = searchOverlay ( state . query , state . caseInsensitive ) ;
295
310
cm . addOverlay ( state . overlay ) ;
@@ -440,7 +455,6 @@ function findNext(cm, rev, callback) {
440
455
function clearSearch ( cm ) {
441
456
cm . operation ( function ( ) {
442
457
var state = getSearchState ( cm ) ;
443
- state . lastQuery = state . query ;
444
458
state . replaceStarted = false ;
445
459
if ( ! state . query ) return ;
446
460
state . query = state . queryText = null ;
0 commit comments