Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active December 21, 2015 00:08
Show Gist options
  • Save mgng/6217436 to your computer and use it in GitHub Desktop.
Save mgng/6217436 to your computer and use it in GitHub Desktop.
簡易わかち書き
function easyWakati(str, key) {
var reg_list = {
'ratin' : '\u0000-\u007F',
'kanji' : '\u4E00-\u9FFF\u3400-\u4DBF\u20000-\u2A6DF\u2A700-\u2B73F\u2B740-\u2B81F\uF900-\uFAFF\u2F800-\u2FA1D\u3190-\u319F\u2E80-\u2EFF\u2F00-\u2FDF\u31C0-\u31EF',
'hiragana' : '\u3040-\u309F',
'katakana' : '\u30A0-\u30FF\u31F0-\u31FF\u1B000-\u1B0FF\uFF65-\uFF9F',
'zen_ascii' : '\uFF00-\uFFEF'
};
var other = [];
for( var k in reg_list ) {
other.push( reg_list[k] );
}
reg_list.other = '^' + other.join('');
var pattern = [];
if ( key in reg_list ) {
pattern.push( '[' + reg_list[key] + ']+' );
} else {
for( var k in reg_list ) {
pattern.push( '[' + reg_list[k] + ']+' );
}
}
return str.match( new RegExp( pattern.join('|'), 'g' ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment