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");
0

1 Answer 1

2

Why not:

var i = "blue";
var p = new RegExp(i,"gi");
var n = str.replace(p, "red");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.