1
var n=str.replace(/blue/g,"red");

This replaces blue in the string to red.

But I get only string blue as a string to replace. I need to replace all instances of it case insensitive as well. I am not able to keep blue in a variable and then use it in replace. I tried this. But it wont work.

var i = blue;
var rep = "/"+i+"/gi";
var n=str.replace(rep,"red");
4

1 回答 1

2

Why not:

var i = "blue";
var p = new RegExp(i,"gi");
var n = str.replace(p, "red");
于 2013-02-18T06:08:54.147 回答