views:

30

answers:

1

Hi, i have this js code :

var str = "javascript:__doPostBack('ctl00$M$List$_rli2$ctl06','')";

alert (str);
var str = str.replace(/\$_rli\d+/, "$_rli" + 7);

alert (str);

And in IE it produces me result as follows:

javascript:__doPostBack('ctl00$M$Listjavascript:__doPostBack('ctl00$M$List$_rli2$ctl06','')rli7$ctl06','')

while it should work like this:

javascript:__doPostBack('ctl00$M$List$_rli7$ctl06','')

and it does in FF, Opera and Chrome.

It Replaces $_ with whole previous string. No escape sequences seems to help.

What am i Doing wrong?

A: 

This is due to the way Internet Explorer handles references in replace. Use $$ instead, which should work in all browsers:

var str = "javascript:__doPostBack('ctl00$M$List$_rli2$ctl06','')";

alert (str);
var str = str.replace(/\$_rli\d+/, "$$_rli" + 7);

alert (str);
// -> javascript:__doPostBack('ctl00$M$List$_rli7$ctl06','')

Alternatively, if you wanted to make it a bit clearer you could use an anonymous function that returns the replacement string:

var str = str.replace(/\$_rli\d+/, function () { return "$_rli" + 7; });

alert (str);
// -> javascript:__doPostBack('ctl00$M$List$_rli7$ctl06','')
Andy E
thanx that was exactly waht i needed and couldn't find