Javascript 大文字・小文字を区別せずに置換する

環境
Google Chrome  115.0.5790.170(Official Build) (64 ビット)
Windows 11 Pro 64bit

構文
文字列.replace(/置換前文字列/gi,’置換後文字列’)
「replace」で正規表現「gi」を使用して、大文字・小文字を区別せずに置換します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const str = 'aBc Abc aBC';
const result = str.replace(/abc/gi, 'stu');
console.log(result);
const str = 'aBc Abc aBC'; const result = str.replace(/abc/gi, 'stu'); console.log(result);
const str = 'aBc Abc aBC';

const result = str.replace(/abc/gi, 'stu');

console.log(result);

実行結果
> “stu stu stu"

JavaScript

Posted by arkgame