Flag /g (global) ใช้กับ regex replace เพื่อแทนที่ ทุกครั้งที่พบ ไม่ใช่แค่ครั้งแรก
ตัวอย่าง ไม่ใช้ /g (แทนที่แค่ครั้งแรก)
<html> <body> <script>
var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/, "red");
document.write(res);
</script> </body> </html>
ผลลัพธ์
Mr Blue has a red house and a blue car
ตัวอย่าง ใช้ /g (แทนที่ทั้งหมด)
<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>
ผลลัพธ์
Mr Blue has a red house and a red car
ยังคง case-sensitive ต้องใช้ /ig ร่วมกันเพื่อแทนที่ทุกครั้งไม่สนใจ case