JavaScript 小文字ごとにスペースで区切るサンプル

構文
文字列.replace(/a-z/g,’ $&’).trim();
正規表現でマッチした小文字文字列を取得します。
trimを使って文字列の両端の空白を削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const str = 'ARkGame';
const res = str.replace(/[a-z]/g,' $&').trim();
console.log(res);
const str = 'ARkGame'; const res = str.replace(/[a-z]/g,' $&').trim(); console.log(res);
const str = 'ARkGame';

const res = str.replace(/[a-z]/g,' $&').trim();

console.log(res);

実行結果
> “AR kG a m e"

JavaScript

Posted by arkgame