0
0

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 1 year has passed since last update.

【javascript】濁音を清音に変換する

Last updated at Posted at 2023-05-03

ひらがなの清音部分を取り出します

String.prototype.normalize()
のNFDを使って、濁音を分解し、一文字目を取り出します。

// 濁音を清音に変換する
function dakuten(str){
    let _str = '';
    for (let i = 0; i < str.length; ++i) {
       _str += str[i].normalize('NFD')[0];
    }
    return _str;
}

const dakuten_arrow = (str: string) => {
    let _str: string = '';
    for (let i = 0; i < str.length; ++i) {
       _str += str[i].normalize('NFD')[0];
    }
    return _str;
}

const str_dakuten: string = Deno.args[0];  // <- "がぎぐげご"
const str_seion = dakuten_arrow(str_dakuten); // もしくは dakuten(str_dakuten);
console.log(str_seion);  // -> "かきくけこ"

関数が2つあるのは同じものです。
下のアロー関数のは、Typescript用に型つけしてあるだけです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?