Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:51:40+00:00 2026-05-27T00:51:40+00:00

I’m creating an auto complete and I’m having a little problem the results highlighting.

  • 0

I’m creating an auto complete and I’m having a little problem the results highlighting. You see my searching algorithm is very generous and ignores apostrophes in the result so therefore a query like joes will match Joe's. The search algorithm is taken care of and now comes the highlighting part. I wanted to make the matched pattern bold.

Let’s say I had an original unformatted result: Joe’s (take note the rsquo \u2019) and I wanted to highlight it like this: Joe’s, for the following queries joes, joe's, and joe’s (rsquo \u2019)

I included the right side single quotation mark in the query because you wouldn’t know if someone was copying a query from a word document or something.

I could easily do it by ignoring the fact that the quotation marks are there in both search/result strings but that would mess up the whole point of the search when you accidentally type something like joes' or even worse jo'es. So I somehow need to preserve the quote position. Also please take note that an apostrophe can also be anywhere in the unformatted result string like so Suq'Ata.

Here are a list of scenarios:


  • String: Liliana's
  • Queries: lilianas, liliana's
  • Result: Liliana's

  • String: Suq'Ata
  • Queries: suqat, suq'at
  • Result: Suq'Ata

  • String: Telim'Tor's
  • Queries: telimt, telim't
  • Result: Telim'Tor's

It should be noted that the position of the quotes in the query is important whereas when you misplace a quote in your query, it should not match at all. So it’s either you have the correct quote position, or no quotes at all to highlight the original string.

I actually don’t mind if a proposed solution is to break apart each letter and loop through it (have thought about it) since I’ll just be doing this to up to 5 strings at a given time. I’m looking forward to your suggestions!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:51:41+00:00Added an answer on May 27, 2026 at 12:51 am

    Updated question specs from asker:

    1. If the user’s query has a quote, its position must match the quote of the original string. Eg. query “jo’es” does not match original string “Joe’s”.
    2. We only need to bother matching the start of the query with the start of the original string.

    I can think of better algorithms, but to start here’s a quick and dirty first stab at it, using the naive loop over each letter 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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.