JS regular expression name highlighting

I am creating an automatic completion, and I have a slight problem with highlighting the results. You see, my search algorithm is very generous and ignores apostrophes as a result, so a query like joes will match Joe's . The search algorithm will take care, and now there is a selection. I wanted to make the offset pattern bold.

Let's say I had the original unformatted result: Joe's (pay attention to rsquo \ u2019), and I wanted to distinguish it as follows: Joe’s for the following queries by joes , Joe's and Joe's (rsquo \ u2019)

In the question, I included a single quotation mark on the right side, because you don’t know if anyone has copied the query from a Word document or something like that.

I could easily do this by ignoring the fact that quotation marks are present in both search / result lines, but it will ruin the entire search point when you accidentally type something like joes' or even worse jo'es , so I somehow you need to keep the quote position. Also note that the apostrophe can also be anywhere in the unformatted result string like this: Suq'Ata .

The following is a list of scenarios:


  • String: Liliana's
  • Requests: lilianas , Liliana's
  • Result: Liliana's

  • String: Suq'Ata
  • Requests: suqat , suq'at
  • Result: Suq'At a

  • String: Telim'Tor's
  • Requests: telimt , telim't
  • Result: Telim'T or's

It should be noted that the position of quotation marks in the request is important, whereas if the quote is incorrect in the request, it should not coincide. So you have the correct quote position or no quotes at all to highlight the original string.

In fact, I do not mind if the proposed solution is to split each letter and break through it (thought about it), since I will do this up to 5 lines at a given time. I look forward to your suggestions!

+4
source share
1 answer

Updated spec specifications from user:

  • If the user's request has a proposal, its position should correspond to the quote of the source string. For instance. the query "jo'es" does not match the source string "Joe's".
  • We only need to coordinate the beginning of the request with the beginning of the source line.

I can think of better algorithms, but here I start a quick and dirty first hit in it, using a naive cycle over each writing method:

 var quotesRegex = /['\u2019]/g; function highlightMatch(origStr, query) { query = query.toLowerCase(); var j = 0; for (var i = 0; i < query.length; ++i, ++j) { // Query has a quote; it needs to be in the same position as origStr if (query.charAt(i).match(quotesRegex)) { if (!origStr.charAt(j).match(quotesRegex)) { return origStr; // quote position mismatch } continue; } while (origStr.charAt(j).match(quotesRegex)) { j++; } if (origStr.charAt(j).toLowerCase() != query.charAt(i)) { return origStr; } } return "<b>" + origStr.slice(0, j) + "</b>" + origStr.slice(j); } 

JSFiddle: http://jsfiddle.net/FFt2T/6/

+1
source

Source: https://habr.com/ru/post/1382868/


All Articles