どうも、ちょげ(@chogetarou)です。
正規表現を使って文字列(string)を1文字ずつに分割する方法を紹介します。
方法

正規表現を使って文字列(string)を1文字ずつに分割するには、split()を使います。
まず、文字列からsplit()を呼び出します。
そして、split()の引数に「/(?!$)/u
」を指定します。
const result = text.split(/(?!$)/u);
上記のsplit()は、呼び出した文字列を1文字ずつに分割した配列を返します。
使用例
const text = "Hello,World";
const result = text.split(/(?!$)/u);
console.log(result);
出力:
[
'H', 'e', 'l', 'l',
'o', ',', 'W', 'o',
'r', 'l', 'd'
]
コメント