どうも、ちょげ(@chogetarou)です。
文字列(string)を空白を全て削除する方法を紹介します。
方法

文字列(string)を空白を全て削除するには、replace()を使います。
まず、文字列からreplace()を呼び出します。
そして、replace()の第1引数に「/\s+/g
」、第2引数に空文字を指定します。
const result = text.replace(/\s+/g, '');
上記のreplace()は、呼び出した文字列の空白を全て削除した文字列を返します。
使用例
const text = " A BC DEF GH IJK ";
const result = text.replace(/\s+/g, '');
console.log("変更前:", text);
console.log("変更後:", result);
出力:
変更前: A BC DEF GH IJK
変更後: ABCDEFGHIJK
コメント