LoginSignup
0
0

More than 3 years have passed since last update.

JavaScriptで平仮名と片仮名判定

Last updated at Posted at 2019-07-12

平仮名を片仮名に変換

const hiraganaToKatakana = (str) => {
  return str.replace(/[\u3041-\u3096]/g, (match) => {
    const chr = match.charCodeAt(0) + 0x60;
    return String.fromCharCode(chr);
  });
};

平仮名のみか判定

const isHiragana = (str) => {
  const regexp = /^[\u{3000}-\u{301C}\u{3041}-\u{3093}\u{309B}-\u{309E}]+$/mu;
  return regexp.test(str);
};

片仮名のみか判定


const isKatakana = (str) => {
  const regexp = /^[\u{3000}-\u{301C}\u{30A1}-\u{30F6}\u{30FB}-\u{30FE}]+$/mu;
  return regexp.test(str);
};
0
0
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
0
0