[英]How to detect if string contains Amharic language in javascript?
我需要檢查一個字符串是否包含阿姆哈拉語,它也可以包含英文字符:
const amharic = "የሙከራ test ሕብረቁምፊ";
amharc.match(pattern)
UTF-16
范圍和charCodeAt()
方法: amharic字母的UTF-16
范圍是4608至5017和11648至11743,因此您可以使用charCodeAt()方法檢查字符串字符是否在這兩個范圍內。
檢查並運行以下代碼片段 ,以獲取上述內容的實際示例:
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
ASCII
范圍和正則表達式: 阿姆哈拉字母的ASCII
范圍是1200
至137F
因此您可以使用正則表達式檢查字符串字符是否在這兩個范圍內。
檢查並運行以下代碼片段 ,以獲取上述內容的實際示例:
var string = "የሙከራ test ሕብረቁምፊ"; function checkAmharic(x) { return /[\ሀ-\]/.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.