I recently needed to detect other languages in JavaScript, specifically I also needed to detect each character, from a different alphabet. There were quite a few hurdles to jump through but I figured it out and I’m happy to share.
I used their Unicode values. This may not be the best way, let me know if there is a better way!
Detecting a single character from another alphabet
For example if you need to find the Hebrew character × you would find it by it’s Unicode \u05d0.
if(value == "\u05d0") { //stuff happens }
Detecting the entire alphabet
If you need to find if any Hebrew was used at all, it would look more like this.
var findhebrew = str.search(/[\u0590-\u05FF]/); if(findhebrew >= 0) { //there is Hebrew in this string }
Only allowing the other alphabet
If you need to only allow Hebrew characters in a string, stripping out the non-Hebrew characters would look like this would look like this.
mystring = mystring.replace(/[^\u0590-\u05FF]/ig,'');
The point is, there is a block of Unicode for each alphabet, which allows you to test for a specific character, or the entire range if you need to.
That’s all folks!
Leave a Reply