JSFiddle - React, Tailwind, and code Playground

JavaScript

var text, searches;

text = "foo \nfoo \nfoo ";
searches = [
 ["just global",                /^foo $/g  ], // finds 0 results
 ["just multiline",             /^foo $/m  ], // finds only 1 result and stops
 ["multiline and global",       /^foo $/mg ], // the only regexp that finds 3 
 ["global with new line",       /foo \n/g  ], // finds 2, but not the last
 ["global with ^ and new line", /^foo \n/g ], // finds only 1, the first
];

searches.forEach(function (search, index) {
  console.log(index);
  printSearches.apply(null, search);
});

function printSearches(label, regex) {
    var res;
    console.log("searching with", label);
    while (res = regex.exec(text)) {
        console.log(" ->  ", label, res, regex.source);
        if (!regex.global) {
            break;
        }
    }
}