skip to Main Content

I have some records in database that are containing Serbian latin letters (ć, č, đ, ž, š).

I’ve done search functionality in backend (nestjs) to search all columns in table that contain typed characters on frontend (react).

How could I search these latin letters when typed character is for example c (this should look for all 3 characters with same "base" -> c, č, ć). Same should be done with the others.

I tried some regex, but never got desired result.

Any basic ideas how should I do this?

3

Answers


  1. Since you have a postgres tag, this can be done using SIMILAR

    You only need to construct your right part, inside %% so as to include these variations

    select * from yourtable
    where yourcolumn SIMILAR TO '%(c|ć|č)%'
    
    Login or Signup to reply.
  2. you can do something like this

    const string = 'this is normal but this ć, č, đ, ž, š not'
    
    const normalizeMap = {
      'č': 'c',
      'ć': 'c',
      'đ': 'd',
      'š': 's',
      'ž': 'z'
    }
    
    const normalize = string => Object
      .entries(normalizeMap)
      .reduce((res, [raw, normalized]) =>  res.replace(new RegExp(raw, 'g'), normalized), string)
    
    console.log(normalize(string))
    Login or Signup to reply.
  3. You can probably utilize localCompare for this.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

    The localeCompare() method returns a number indicating whether a
    reference string comes before, or after, or is the same as the given
    string in sort order. In implementations with Intl.Collator API
    support, this method simply calls Intl.Collator.

    A negative number if referenceStr occurs before compareString;
    positive if the referenceStr occurs after compareString; 0 if they are
    equivalent.

    An example:

    let a = 'caknut';
    let b = 'ćaknut';
    console.log(a.localeCompare(b, 'sr', { sensitivity: 'base' })); // 0
    

    A 0 indicates that both strings are equivalent.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search