JavaScript là ngôn ngữ phân biệt chữ hoa thường (case sensitive). Khi thay thế từ, cần kiểm tra chữ hoa/thường. Với một từ thì dễ, nhưng nhiều từ thì phức tạp. Cờ "/i" xuất hiện để giải quyết. Nó thay thế từ bất kể chữ hoa/thường.
Ví dụ
Thay tất cả "blue" thành "red". Một từ viết hoa "Blue" nên không bị thay (trừ khi dùng /i).
<html> <body> <script>
var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/g, "red");
document.write(res);
</script> </body> </html>
Kết quả
Mr Blue has a red house and a red car
Khi dùng cờ "/i", mọi từ "blue" bất kể hoa/thường đều thành "red".
Ví dụ
<html> <body> <script>
var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/ig, "red");
document.write(res);
</script> </body> </html>
Kết quả
Mr red has a red house and a red car