@@ -180,6 +180,7 @@ export class SearchState {
180180
181181 private _matchesDocVersion : number ;
182182 private _searchDirection : SearchDirection = SearchDirection . Forward ;
183+ private isRegex : boolean ;
183184
184185 private _searchString = "" ;
185186 public get searchString ( ) : string {
@@ -214,7 +215,21 @@ export class SearchState {
214215 ignorecase = false ;
215216 }
216217
217- const regex = new RegExp ( search . replace ( SearchState . specialCharactersRegex , "\\$&" ) , ignorecase ? 'gi' : 'g' ) ;
218+ let searchRE = search ;
219+ if ( ! this . isRegex ) {
220+ searchRE = search . replace ( SearchState . specialCharactersRegex , "\\$&" ) ;
221+ }
222+
223+ const regexFlags = ignorecase ? 'gi' : 'g' ;
224+
225+ let regex : RegExp ;
226+ try {
227+ regex = new RegExp ( searchRE , regexFlags ) ;
228+ } catch ( err ) {
229+ // Couldn't compile the regexp, try again with special characters escaped
230+ searchRE = search . replace ( SearchState . specialCharactersRegex , "\\$&" ) ;
231+ regex = new RegExp ( searchRE , regexFlags ) ;
232+ }
218233
219234 outer:
220235 for ( let lineIdx = 0 ; lineIdx < TextEditor . getLineCount ( ) ; lineIdx ++ ) {
@@ -228,7 +243,7 @@ export class SearchState {
228243
229244 this . matchRanges . push ( new vscode . Range (
230245 new Position ( lineIdx , result . index ) ,
231- new Position ( lineIdx , result . index + search . length )
246+ new Position ( lineIdx , result . index + result [ 0 ] . length )
232247 ) ) ;
233248
234249 if ( result . index === regex . lastIndex ) {
@@ -281,10 +296,11 @@ export class SearchState {
281296 }
282297 }
283298
284- constructor ( direction : SearchDirection , startPosition : Position , searchString = "" ) {
299+ constructor ( direction : SearchDirection , startPosition : Position , searchString = "" , { isRegex = false } = { } ) {
285300 this . _searchDirection = direction ;
286301 this . _searchCursorStartPosition = startPosition ;
287302 this . searchString = searchString ;
303+ this . isRegex = isRegex ;
288304 }
289305}
290306
0 commit comments