She*_*vel 2 javascript regex unicode unicode-string
我需要检查一个字符串是否包含阿姆哈拉语,它也可以包含英语字符:
\n\nconst amharic = "\xe1\x8b\xa8\xe1\x88\x99\xe1\x8a\xa8\xe1\x88\xab test \xe1\x88\x95\xe1\x89\xa5\xe1\x88\xa8\xe1\x89\x81\xe1\x88\x9d\xe1\x8d\x8a";\namharc.match(pattern)\nRun Code Online (Sandbox Code Playgroud)\n
UTF-16范围及charCodeAt()方法:UTF-16阿姆哈拉语字母的范围是从4608到5017以及从11648到11743,因此您可以使用charCodeAt()方法来检查字符串字符是否在这两个范围内。
检查并运行以下代码片段以获取我上面描述的实际示例:
\n\nvar string = "\xe1\x8b\xa8\xe1\x88\x99\xe1\x8a\xa8\xe1\x88\xab test \xe1\x88\x95\xe1\x89\xa5\xe1\x88\xa8\xe1\x89\x81\xe1\x88\x9d\xe1\x8d\x8a";\r\n\r\nfunction checkAmharic(x) {\r\n let flag = false; \r\n [...x].forEach((e, i) => {\r\n \tif (((e.charCodeAt(i) > 4607) && (e.charCodeAt(i) < 5018)) || ((e.charCodeAt(i) > 11647) && (e.charCodeAt(i) < 11743))) {\r\n \tif (flag == false) {\r\n \tflag = true;\r\n }\r\n }\r\n })\r\n return flag; \r\n}\r\n\r\nconsole.log(checkAmharic(string)); // will return true\r\nconsole.log(checkAmharic("Hello All!!")); // will return falseRun Code Online (Sandbox Code Playgroud)\r\nASCII范围和正则表达式:ASCII阿姆哈拉语字母的范围是从1200到137F因此您可以使用正则表达式来检查字符串字符是否落在这两个范围内。
检查并运行以下代码片段以获取我上面描述的实际示例:
\n\nvar string = "\xe1\x8b\xa8\xe1\x88\x99\xe1\x8a\xa8\xe1\x88\xab test \xe1\x88\x95\xe1\x89\xa5\xe1\x88\xa8\xe1\x89\x81\xe1\x88\x9d\xe1\x8d\x8a";\r\n\r\nfunction checkAmharic(x) {\r\n return /[\\u1200-\\u137F]/.test(x); // will return true if an amharic letter is present\r\n}\r\n\r\nconsole.log(checkAmharic(string)); // will return true\r\nconsole.log(checkAmharic("A")); // will return falseRun Code Online (Sandbox Code Playgroud)\r\n