0

I have an ASP.NET app with openlayers and JS, inside the web map app I have a seachbox where the text I enter is modified to match different characters (e.g. the character 'i' can be 'i', 'í' or 'ì'). Also I need to find different words that can be incomplete and separated, so if I search 'graf de orca cio' the text below should appear as a result:

"Base topogràfica de l'illa de Menorca, realitzada a partir de l'actualització"

This becomes slower when I search trough hundreds (between 100 and 200) of capabilities like this one below:

https://ideib.caib.es/geoserveis/services/public/GOIB_MTIB5m2012_Me/MapServer/WMSServer/?service=wms&request=getcapabilities

I search the fields inside the tags 'Name', 'Title', 'Keyword' and 'Abstract', the last one seems to be the one with longer texts.

The text I enter goes through a piece of code so "graf de orca cio" becomes ".*?(gr(a|á|à)f).*?(d(e|é|è)).*?((o|ó|ò)rc(a|á|à)).*?(c(i|í)(o|ó|ò)).*?" and that is the regular expression used to search. The code i use to make the transformation is this:

            this.patternFn = function (t) {

            t = t.replace(/[^a-z\dáàéèíóòúüñ]/gi, '\\' + '$&');
            t = t.replace(/(a|á|à)/gi, "(a|á|à)");
            t = t.replace(/(e|é|è)/gi, "(e|é|è)");
            t = t.replace(/(i|í)/gi, "(i|í)");
            t = t.replace(/(o|ó|ò)/gi, "(o|ó|ò)");
            t = t.replace(/(u|ú|ü)/gi, "(u|ú|ü)");
            t = t.replace(/n/gi, "(n|ñ)");
            t = ".*?(" + t + ").*?";
            t = t.replace(/\\ /gi, ").*?(");

            return t;

This whole thing seems to be not the most efficient way to do this, so sometimes the search freezes the app some seconds. Any idea on how to make this work faster?

mrvinent
  • 115
  • 2
  • 16
  • There is [a better way to escape special chars](https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex), you do not need to escape every non alnum chars with `.replace(/[^a-z\dáàéèíóòúüñ]/gi, '\\' + '$&')`. Also, do not use groups if you mean character classes, `(o|ó|ò)` => `[oóò]`. The worst are probably the last two lines though. `t.replace(/\\ /gi, ").*?(");` will make it slow regardless of what you are trying. – Wiktor Stribiżew Jun 05 '20 at 08:49
  • @WiktorStribiżew This code comes from an API, the last two lines are a modification I did to match incomplete and/or separated words. I guess that the original code was the most efficient option available. – mrvinent Jun 05 '20 at 09:32
  • If you match words, replace `.` with `\S` (any non-whitespace) or `\w` (any ASCII letter, digit or `_`). If you mean to match whitespace between words, replace `.*?` with `\s*` – Wiktor Stribiżew Jun 05 '20 at 09:40

0 Answers0