@@ -274,22 +274,37 @@ function parseString(string) {
274274 } ) ;
275275}
276276
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 ;
283281 } 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' ) ;
285300 }
286- if ( typeof query == 'string' ? query == '' : query . test ( '' ) ) query = / x ^ / ;
287- return query ;
301+ return regexp ;
288302}
289303
290304function startSearch ( cm , state , query ) {
291305 state . queryText = query ;
292- state . query = parseQuery ( query ) ;
306+ state . lastQuery = query ;
307+ state . query = parseQuery ( query , state ) ;
293308 cm . removeOverlay ( state . overlay , state . caseInsensitive ) ;
294309 state . overlay = searchOverlay ( state . query , state . caseInsensitive ) ;
295310 cm . addOverlay ( state . overlay ) ;
@@ -440,7 +455,6 @@ function findNext(cm, rev, callback) {
440455function clearSearch ( cm ) {
441456 cm . operation ( function ( ) {
442457 var state = getSearchState ( cm ) ;
443- state . lastQuery = state . query ;
444458 state . replaceStarted = false ;
445459 if ( ! state . query ) return ;
446460 state . query = state . queryText = null ;
0 commit comments