How can I match Korean characters in a Ruby regular expression?

Jakub Arnold picture Jakub Arnold · Apr 13, 2012 · Viewed 8.4k times · Source

I have some basic validations for usernames using regular expressions, something like [\w-_]+, and I want to add support for Korean alphabet, while still keeping the validation the same.

I don't want to allow special characters, such as {}[]!@#$%^&*() etc., I just want to replace the \w with something that matches a given alphabet in addition to [a-zA-Z0-9].

Which means username like 안녕 should be valid, but not 안녕[].

I need to do this in Ruby 1.9.

Answer

김민준 picture 김민준 · Feb 21, 2014

try this:

[가-힣]+

This matches every character from U+AC00 to U+D7A3, which is probably enough for your interest. (I don't think you'll need old hangul characters and stuff)