How to Convert Hiragana and Katakana in React
In Unicode, hiragana and katakana are laid out in the same order, and their code points are exactly 0x60 apart. So adding 0x60 to each character from ぁ to ゖ gives katakana, and subtracting 0x60 from katakana gives hiragana. Only characters outside this range, such as ヷ and half-width kana, need extra handling.
Here I put the conversion functions in kana.ts, and built two components: KanaConverter, which converts with buttons, and FuriganaField, which converts to katakana when the field loses focus.
Test environment: Ubuntu 24.04, Node.js 22.23.4, Chromium 153. I used react / react-dom 19.3.1, @types/react 19.3.0, typescript 7.0.2, esbuild 0.28.2, wanakana 5.3.1, and Playwright 1.63.0. I checked the function results in Node.js and confirmed there were no type errors with tsc. I ran the components in a development build wrapped in StrictMode, and operated them from Playwright in Chromium at a 375px width. I did not test typing with an IME (Japanese input), Firefox, Safari, or real smartphones.

A function that shifts character codes by 0x60
The conversion functions are below. They select only the characters to convert with a regular expression, and shift their character codes.
// Converts hiragana and katakana by shifting the character codes by 0x60
const OFFSET = 0x60;
// Converts hiragana U+3041 to U+3096, plus the iteration marks U+309D and U+309E, to katakana
export function toKatakana(input: string): string {
return (
input
.replace(/[\u3041-\u3096\u309D\u309E]/g, (c) =>
String.fromCharCode(c.charCodeAt(0) + OFFSET),
)
// If the hiragana wa, wi, we, or wo is followed by a dakuten (U+3099), merge the pair into one katakana character (U+30F7 to U+30FA)
.replace(/([\u30EF-\u30F2])\u3099/g, (_, c: string) =>
String.fromCharCode(c.charCodeAt(0) + 8),
)
);
}
// Converts katakana U+30A1 to U+30F6, plus the iteration marks U+30FD and U+30FE, to hiragana
export function toHiragana(input: string): string {
return (
input
// There is no single-character hiragana for U+30F7 to U+30FA, so convert each to a wa-row kana followed by the dakuten (U+3099)
.replace(/[\u30F7-\u30FA]/g, (c) =>
String.fromCharCode(c.charCodeAt(0) - 8 - OFFSET) + '\u3099',
)
.replace(/[\u30A1-\u30F6\u30FD\u30FE]/g, (c) =>
String.fromCharCode(c.charCodeAt(0) - OFFSET),
)
);
}
// Converts only the half-width katakana (U+FF61 to U+FF9F) to full width. The pair ガ becomes the single character U+30AC
export function halfwidthKanaToFullwidth(input: string): string {
return input.replace(/[\uFF61-\uFF9F]+/g, (s) => s.normalize('NFKC'));
}
// Checks whether the string contains the query, ignoring the difference between hiragana and katakana
export function includesIgnoringKana(text: string, query: string): boolean {
const fold = (s: string) => toHiragana(halfwidthKanaToFullwidth(s));
return fold(text).includes(fold(query));
}
When I ran toKatakana('ねこの てちょう') in Node.js, I got ネコノ テチョウ. The small ぁ, っ, ゃ, and ゎ also became the small katakana ァ, ッ, ャ, and ヮ. ゔ, ゕ, and ゖ changed to ヴ, ヵ, and ヶ.
Conversely, toHiragana('ヴァイオリンと ラーメン') gives ゔぁいおりんと らーめん. I left the long vowel mark ー (U+30FC) unchanged, because its Unicode name is KATAKANA-HIRAGANA PROLONGED SOUND MARK, and it is used in both hiragana and katakana. The middle dot ・ (U+30FB) is outside the conversion range, so it also stays. Tokyo 東京 2026 did not change with either toHiragana() or toKatakana().
Characters outside the ranges ぁ–ゖ and ァ–ヶ
I handled the kana-related characters outside the ranges ぁ–ゖ (U+3041 to U+3096) and ァ–ヶ (U+30A1 to U+30F6) as follows.
| Character | Code point | Handling |
|---|---|---|
ゝ
ゞ / ヽ
ヾ |
U+309D to U+309E / U+30FD to U+30FE | Iteration marks. They are 0x60 apart, so I included them in the range and convert them |
ヷ
ヸ
ヹ
ヺ |
U+30F7 to U+30FA | No single-character hiragana exists. Converted to わ-row kana plus the combining dakuten (U+3099), two characters |
ゟ |
U+309F | HIRAGANA DIGRAPH YORI, a single character for yori. Left as is |
ヿ |
U+30FF | KATAKANA DIGRAPH KOTO, a single character for koto. Left as is |
ㇰ
ㇱ
ㇲ etc. |
U+31F0 onward | Additional small katakana (e.g., KATAKANA LETTER SMALL KU). They are outside the 0x60 range, so they are left as is |
カ
゙ etc. |
U+FF61 to U+FF9F | Half-width kana. Converted to full width first with halfwidthKanaToFullwidth() |
ヷ (U+30F7) minus 0x60 gives U+3097, but no character is assigned there. In the Node.js test environment it matched /\p{Cn}/u (unassigned characters), and Python's unicodedata also had no name for it. So I convert ヷ into two characters: わ (U+308F) followed by the combining dakuten (U+3099). In toKatakana(), when these two characters appear together, I merge them back into the single ヷ. In the test environment, toKatakana(toHiragana('ヷ')) returned ヷ, so it round-tripped correctly.
For merging ワ and the dakuten in toKatakana(), I used a regular expression. normalize('NFC') also merged the pair ワ (U+30EF) and the dakuten (U+3099) into one ヷ (U+30F7) in the test environment. As the MDN description of normalize() explains, NFC decomposes and then recomposes the string, so a pair that has a precomposed character becomes one character. However, applying NFC to the whole string changes other characters too. In the test environment, the CJK compatibility ideograph U+FA19 (CJK COMPATIBILITY IDEOGRAPH-FA19, which looks like 神) became the regular 神 (U+795E). So I replace only the places where ワ, ヰ, ヱ, or ヲ is followed by the dakuten.
When I passed か followed by the combining dakuten (U+3099), which is written が, to toKatakana(), it became カ followed by the dakuten, two characters. The dakuten is outside the range, so it stays.
Convert half-width kana to full width first
toHiragana('ネコ') returns ネコ unchanged. Half-width kana have a different code point order from full-width kana, so shifting by 0x60 cannot convert them. halfwidthKanaToFullwidth() applies normalize('NFKC') only to the half-width part, making it full width, and then the conversion runs. In the test environment, ネコ ガイド ①㈱ became ネコ ガイド ①㈱. The two characters ガ merged into one ガ, and ① and ㈱, which are outside the half-width range, stayed as they were.
Input field that converts with buttons
KanaConverter converts the text in the field to hiragana or katakana with buttons.
import { useId, useState } from 'react';
import { toHiragana, toKatakana } from './kana';
export function KanaConverter({ initial = '' }: { initial?: string }) {
const id = useId();
const [text, setText] = useState(initial);
const [message, setMessage] = useState('');
function convert(to: 'hiragana' | 'katakana') {
const next = to === 'hiragana' ? toHiragana(text) : toKatakana(text);
const label = to === 'hiragana' ? 'ひらがな' : 'カタカナ';
setMessage(next === text ? `${label}にする文字はありませんでした` : `${label}にしました`);
setText(next);
}
return (
<div className="kana-converter">
<label htmlFor={`${id}-text`}>変換する文字</label>
<textarea
id={`${id}-text`}
rows={3}
value={text}
onChange={(e) => {
setText(e.target.value);
setMessage('');
}}
/>
<div className="buttons">
<button type="button" onClick={() => convert('hiragana')}>
ひらがなにする
</button>
<button type="button" onClick={() => convert('katakana')}>
カタカナにする
</button>
</div>
<p role="status">{message}</p>
</div>
);
}
.kana-converter, .furigana-field { display: grid; gap: 8px; max-width: 480px; }
.kana-converter textarea, .furigana-field input { font-size: 16px; }
.kana-converter .buttons { display: flex; gap: 8px; }
.kana-converter [role='status'] { margin: 0; min-height: 1.5em; color: #067a98; }
If the text is the same before and after conversion, nothing was changed. In that case the component shows a message such as ひらがなにする文字はありませんでした. The result message is shown in a <p> with role="status", and it is cleared when the user types in the field.
I set the initial value to two lines, ねこの てちょう and ヴァイオリンと ラーメン, and pressed the buttons with Playwright. Pressing ひらがなにする changed the second line to ゔぁいおりんと らーめん and showed ひらがなにしました. Pressing it again left the text unchanged and showed ひらがなにする文字はありませんでした. Then pressing カタカナにする gave the two lines ネコノ テチョウ and ヴァイオリント ラーメン.
The function does not look at the meaning of the sentence, so particles such as の and と also become katakana. If you want to change only some words in a sentence, you have to select that part before converting, or fix the text by hand after converting.
Convert to katakana when the furigana field loses focus
A furigana field may be typed in hiragana, but you may want its value to be full-width katakana when it is submitted. In FuriganaField, I did not change the value while typing. The conversion runs once, when the field loses focus (onBlur).
import { useId, useState } from 'react';
import { halfwidthKanaToFullwidth, toKatakana } from './kana';
// Furigana field. Leaves the value alone while typing, and normalizes it to full-width katakana when the field loses focus
export function FuriganaField({ name }: { name: string }) {
const id = useId();
const [value, setValue] = useState('');
return (
<div className="furigana-field">
<label htmlFor={id}>フリガナ</label>
<input
id={id}
name={name}
value={value}
onChange={(e) => setValue(e.target.value)}
onBlur={() => setValue(toKatakana(halfwidthKanaToFullwidth(value)))}
/>
</div>
);
}
I placed this field inside a <form> and, with Playwright, typed やまだ タロウ one character at a time. Right after typing, the field still showed やまだ タロウ. When I left the field with the Tab key, it changed to ヤマダ タロウ. The value I read with get('kana') from a FormData built from the form was also ヤマダ タロウ. The half-width タロウ was converted to full-width katakana as well.
In onChange, I only store the typed characters in state. The conversion runs just once, when the field loses focus.
Comparing with the wanakana library
Hiragana and katakana conversion can also be done with toHiragana() and toKatakana() from the wanakana library. For some of the same inputs, the results differed.
| Input | wanakana | Functions in this article |
|---|---|---|
toHiragana('ラーメン') |
らあめん |
らーめん |
toHiragana('ヷ') |
U+3097 (unassigned) | わ followed by the dakuten (two characters) |
toHiragana('ヵヶ') |
ヵヶ (unchanged) |
ゕゖ |
toHiragana('ヽヾ') |
ヽヾ (unchanged) |
ゝゞ |
toKatakana('ねこ neko') |
ネコ ネコ |
ネコ neko |
wanakana's toHiragana() turned the long vowel in ラーメン into あ, giving らあめん. Also, wanakana is a library that converts romaji to kana, so its toKatakana() converts the English letters in neko to ネコ too. As the wanakana README describes, to keep the Latin letters you pass { passRomaji: true }.
Searching without distinguishing hiragana and katakana
When searching a list for ねこ, if you also want to find ネコ, normalize both sides to hiragana and then use includes(). includesIgnoringKana() in kana.ts does this, and it converts half-width kana to full width before comparing. In the test environment, the results were as follows.
| Target string | Search term | Result |
|---|---|---|
ネコの手帳 |
ねこ |
true |
ねこのてちょう |
ネコ |
true |
ネコ ショップ |
ねこ |
true |
ラーメン |
らあめん |
false |
ラーメン becomes らーめん, so it is not found with らあめん. If you also want to treat the long vowel as equal, you need an extra step that replaces ー with the vowel of the preceding character.
If you only need to check whether two strings are equal, you can also compare them with Intl.Collator. In the Node.js test environment, new Intl.Collator('ja').compare('ねこ', 'ネコ') returned 0, so they were treated as equal. I tried all four sensitivity values from the MDN Intl.Collator description (base, accent, case, and variant), and every one returned 0. Half-width ネコ also returned 0. The dakuten difference between ねこ and ねご was distinguished with accent and variant, and treated as equal with base and case.
However, compare() compares whole strings, so it cannot tell whether ねこ appears inside ネコの手帳. Also, when I sorted ['ネコ', 'ねこ', 'ねご', 'ネコ'] with this compare, the result was ['ネコ', 'ねこ', 'ネコ', 'ねご']. Hiragana and katakana remained mixed in the original order.





