更新:
/ 作成: util.tsexport class Util {
/**
* 文字列に含まれているすべてのひらがなをカタカナに変換した文字列を作成します。
* ただし、半角文字は変換しません。
* @param str ひらがなを含む文字列
*/
static hiraToKata(str: string): string {
return str.replace(/[\u3041-\u3096]/g, ch =>
String.fromCharCode(ch.charCodeAt(0) + 0x60)
);
}
/**
* 文字列に含まれているすべてのカタカナをひらがなに変換した文字列を作成します。
* ただし、半角文字は変換しません。
* @param str カタカナを含む文字列
*/
static kataToHira(str: string): string {
return str.replace(/[\u30A1-\u30FA]/g, ch =>
String.fromCharCode(ch.charCodeAt(0) - 0x60)
);
}
}
使用例import { Util } from './util';
console.log(Util.hiraToKata('あいうアイウ')); //=> アイウアイウ
console.log(Util.kataToHira('あいうアイウ')); //=> あいうあいう
関連記事