Viglino/ol-ext

Escaping characters in SearchField?

Closed this issue · 2 comments

Is there a way to escape certain characters in the user input given to ol-ext/control/SearchFeature? My use case is searching for room identifiers that take the form '1.23' or '101'. Currently searching for '1.1' treats the dot as regex so returns 101, 111, etc. Searching for '1.1' returns the desired results, e.g. 1.10, 1.11 etc but isn't terribly user-friendly. I'd like to escape the dot in the code if possible.

You have to write your own control as a subclass of the auto SearchFeature and overwrite the autocomplete to escape char:

// My control
class mySearchFeature extends ol.control.SearchFeature {
  autocomplete(s) {
     var s2 = escape(s)
     return ol.control.SearchFeature.prototype.autocomplete.call(this, s2)
  }
}

// add a control to map
var search = new mySearchFeature(...)
map.addControl(search)

or

import SearchFeature from 'ol-ext/control/SearchFeature'

// My control
class mySearchFeature extends SearchFeature {
  autocomplete(s) {
     var s2 = escape(s)
     return SearchFeature.prototype.autocomplete.call(this, s2)
  }
}

// add a control to map
const search = new mySearchFeature(...)
map.addControl(search)

Thank you very much for your prompt response which solves my problem. The only change I made was to replace escape(s) with s.replace('.', '\\.').