2

I need to check if a string contains Amharic language, it can contain English characters as well:

const amharic = "የሙከራ test ሕብረቁምፊ";
amharc.match(pattern)
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • 3
    Possible duplicate of [How to check if any Arabic character exists in the string ( javascript )](https://stackoverflow.com/questions/4446244/how-to-check-if-any-arabic-character-exists-in-the-string-javascript) – Web Nexus Jan 27 '19 at 10:21

1 Answers1

5

Using UTF-16 range and the charCodeAt() method:

The UTF-16 range for the amharic letters are from 4608 to 5017 and from 11648 to 11743 so you can use the charCodeAt() method to check if the string characters fall within those two ranges or not.


Check and run the following Code Snippet for a practical example of what I described above:

var string = "የሙከራ test ሕብረቁምፊ";

function checkAmharic(x) {
    let flag = false;   
    [...x].forEach((e, i) => {
     if (((e.charCodeAt(i) > 4607) && (e.charCodeAt(i) < 5018)) || ((e.charCodeAt(i) > 11647) && (e.charCodeAt(i) < 11743))) {
       if (flag == false) {
         flag = true;
        }
      }
    })
    return flag; 
}

console.log(checkAmharic(string)); // will return true
console.log(checkAmharic("Hello All!!")); // will return false

Using ASCII range and regex:

The ASCII range for the amharic letters are from 1200 to 137F so you can use regex to check if the string characters fall within those two ranges or not.


Check and run the following Code Snippet for a practical example of what I described above:

var string = "የሙከራ test ሕብረቁምፊ";

function checkAmharic(x) {
    return /[\u1200-\u137F]/.test(x); // will return true if an amharic letter is present
}

console.log(checkAmharic(string)); // will return true
console.log(checkAmharic("A")); // will return false
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • Does the test return TRUE if an ascii 'A' is found ? –  Jan 27 '19 at 16:30
  • @sln No, it returns false. I have updated the answer to include testing a string which has "A" to show this. I have also added an alternative JavaScript approach that uses `UTF-16` instead to check the string for `amharic` letters. Cheers. – AndrewL64 Jan 27 '19 at 17:21