51
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javascript 日本語チェックです

Last updated at Posted at 2017-03-22

.match()を使い正規表現をチェックします。

漢字・ひらがな・カタカナ以外の文字を含んでいたら「false」が返ります。

function ja2Bit ( str ) {
  return ( str.match(/^[\u30a0-\u30ff\u3040-\u309f\u3005-\u3006\u30e0-\u9fcf]+$/) )? true : false
}

ja2Bit('あいうえお') //true
ja2Bit('アイウエオ') //true
ja2Bit('漢字') //true
ja2Bit('abcdefg') // false
ja2Bit('あいうえおアイウエオ漢字') // true
ja2Bit('あいうえおアイウエオ漢字abcdefg') //false

カタカナ
\u30a0-\u30ff

ひらがな
\u3040-\u309f

々, 〆(記号)
\u3005-\u3006

CJK統合漢字
\u30e0-\u9fcf

「々」って記号なんですね~

参考

51
45
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?