TypeScript 文字列の空白を削除するサンプル

環境
Windows 11 Pro 64bit
TypeScript 4.4.4

構文
const result: string = 文字列.replace(/\s+/g, ")
replace()の第1引数に「/\s+/g」、第2引数に空文字を指定します。
呼び出した文字列(string)の空白を削除した文字列を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const tstr: string = " S  t  u S K a "
const result: string = tstr.replace(/\s+/g,'')
console.log(result)
const tstr: string = " S  t  u S K a " const result: string = tstr.replace(/\s+/g,'') console.log(result)
const tstr: string = "   S   t  u S  K  a "

const result: string = tstr.replace(/\s+/g,'')

console.log(result)

実行結果
[LOG]: “StuSKa"

TypeScript

Posted by arkgame