Many questions you might think are related will say something like "use /g". That's not what I'm after. There must be a way to do this. What I have is the following:
<script>
var myString = "DECLARE DeCLARE Declare DECLARE";
var arr = myString.match(new RegExp("[Dd]eclare|DECLARE","gm"));
var clr = "#F00";
for(var i=0; i<arr.length; i++)
{
myString = myString.replace(arr[i],"-"+arr[i]+"-");
}
document.write(myString);
</script>
The output:
--DECLARE-- DeCLARE -Declare- DECLARE
Desired output:
-DECLARE- DeCLARE -Declare- -DECLARE-