Monday, May 30, 2016

Javascript: Thai Charactor Checking using Regular Expression

Here is a code to check thai character (ภาษาไทย) using regular expression.
Regular expression is one of methods to check string pattern. It provides shortcut word to check english charactor like [A-Z] or [a-z]. Unfortunately, it does not easy to do like this in thai language (ภาษาไทย).
So, I do some research and found how to check thai character using regular expression.

Code below is testing if whole string contains only english and thai charactor:
var re = new RegExp("^([A-Z]|[a-z]|[0-9]|[/]|[\\]|[ ]|[\n]|[.]|[ๅภถุึคตจขชๆไำพะัีรนยบลฃฟหกดเ้่าสวงผปแอิืทมใฝ๑๒๓๔ู฿๕๖๗๘๙๐ฎฑธํ๊ณฯญฐฅฤฆฏโฌ็๋ษศซฉฮฺ์ฒฬฦ])+$", "g");

//return true.
console.log(re.test("วิทยาลัยดุริยางคศิลป์ มหาวิทยาลัยมหิดล 25/25 ตำบลศาลายา อำเภอพุทธมณฑล นครปฐม Thialand 73170."));  

//return false.
console.log(re.test("$ XYZ ทดสอบ 12345."));  

Credit: This is modified version of Ninenik Narkdee which creates thai charactor checking function.



Happy Coding!

1 comment:

  1. คะแนนที่แคะแนนที่แคะแนนที่แคะแนนที่แคะแนนที่แ

    ReplyDelete