javascriptjqueryhtmlregexarabic

Regex with Arabic expressions


How do I make Javascript ignore the Arabic expression اعراب through Regex? For example I want that و and ؤ be equal and ا آ اَ اِ to be all equal and so on. Please help. Thanks a lot.


Solution

  • i could suggest using ReplaceAll() with a regex for multiple characters at both result and input.

    check the function called rn(name)

    e.g:

    let input = document.getElementById('search')
    let list = document.getElementById('names')
    
    let rn = (name) => {
      return name.replaceAll(/[أإآ]/g, 'ا').replaceAll('ؤ', 'و').replaceAll('ة', 'ه').replaceAll('ى', 'ي')
    }
    
    input.addEventListener('keyup', (e) => {
      for (i = 0; i < list.children.length; i++) {
        if (e.target.value && rn(list.children[i].innerText).includes(rn(e.target.value))) list.children[i].style = 'display:block'
        else list.children[i].style = 'display:none'
      }
    })
    li {
      display: none
    }
    <input id='search' type='text' list='names' placeholder='Type name..' />
    <ul id='names'>
      <li>أحمد</li>
      <li>أيمن</li>
      <li>إبراهيم</li>
      <li>إيمان</li>
      <li>آية</li>
      <li>آيات</li>
      <li>بوسي</li>
      <li>رؤى</li>
      <li>هدى</li>
      <li>يمنى</li>
      <li>سلمى</li>
      <li>شريفة</li>
      <li>ميادة</li>
      <li>تقى</li>
    </ul>